vvangelovski / django-audit-log

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

How can `modified_by` be manually set/updated? #38

Open jordanmkoncz opened 8 years ago

jordanmkoncz commented 8 years ago

In my application I have a model, let's call it ModelA, which does not use the AuthStampedModel mixin. I have another model, ModelB which uses the AuthStampedModel mixin.

Part of the functionality of my application includes handling a POST request that makes an update to a ModelA record, and then also makes an update to an associated ModelB record (based on application-specific business logic). When I make the update to the ModelB record I want its modified_by value to be set to the user who triggered the update to the ModelA record which then led to also updating the ModelB record.

However I'm stuck on how I would accomplish this. I've tried doing the following:

user = model_a_record.user
model_b_record.modified_by = user
model_b_record.save()

However this attempt to manually update modified_by does not work. I assumed this would be because modified_by is a LastUserField which has a default of editable=False, but even after creating my own EditableAuthStampedModel where the modified_by field is set to a LastUserField with an explicit value of editable=True, the modified_by value that I manually set does not get saved.

Any ideas?

jordanmkoncz commented 8 years ago

For additional context, the code where I'm attempting to do this is part of a Django api_view, which looks like this:

@api_view(['POST'])
@authentication_classes([permissions.IsAuthenticated])
def submit_model_a(request, format=None):
    #  Code handling ModelA submission

    user = model_a_record.user
    model_b_record.modified_by = user
    model_b_record.save()

    # Code handling returning a response

I'm wondering whether perhaps the issue is that django-audit-log's UserLoggingMiddleware is coming in to play when I try to save the ModelB record and is basically overwriting the modified_by value with None.