Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can modified_by be manually set/updated? #38

Open
jordanmkoncz opened this issue Jul 11, 2016 · 1 comment
Open

How can modified_by be manually set/updated? #38

jordanmkoncz opened this issue Jul 11, 2016 · 1 comment

Comments

@jordanmkoncz
Copy link

jordanmkoncz commented Jul 11, 2016

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
Copy link
Author

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.

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

No branches or pull requests

1 participant