vvangelovski / django-audit-log

Audit log for your Django models
Other
232 stars 93 forks source link

Do not overwrite creating fields if were set explicitly #62

Open st4lk opened 5 years ago

st4lk commented 5 years ago

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.