Skip to content

Do not overwrite creating fields if were set explicitly#62

Open
st4lk wants to merge 4 commits intovvangelovski:masterfrom
st4lk:do-not-overwrite-creating-fields
Open

Do not overwrite creating fields if were set explicitly#62
st4lk wants to merge 4 commits intovvangelovski:masterfrom
st4lk:do-not-overwrite-creating-fields

Conversation

@st4lk
Copy link

@st4lk st4lk commented Feb 28, 2019

Sometimes, I'd like to specify different user for CreatingUserField field by myself. Such cases are rare, usually I'm ok with populating this field from request.user automatically. But they still happens.

Example:

from audit_log.models.fields import CreatingUserField

class MyModel(models.Model):

    created_by = CreatingUserField(...)

# --------------------

def my_view(request):

    if request.method == 'POST':
        # created_by will be filled from request.user, that is ok
        MyModel.objects.create()
        
        # but here I'd like to change the created_by user.
        # I can't do this because it will be always
        # equal to request.user. Since I've specified the user explicitly - audit_log
        # should assume that I know what I'm doing.
        another_user = User.objects.get(id=123)
        MyModel.objects.create(created_by=another_user)

Solution: fill CreatingUserField automatically only if no value was provided. Otherwise - don't overwrite it.

This PR miss the tests. I'll add them if such behaviour will be approved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant