You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
The text was updated successfully, but these errors were encountered:
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.
In my application I have a model, let's call it
ModelA
, which does not use theAuthStampedModel
mixin. I have another model,ModelB
which uses theAuthStampedModel
mixin.Part of the functionality of my application includes handling a
POST
request that makes an update to aModelA
record, and then also makes an update to an associatedModelB
record (based on application-specific business logic). When I make the update to theModelB
record I want itsmodified_by
value to be set to the user who triggered the update to theModelA
record which then led to also updating theModelB
record.However I'm stuck on how I would accomplish this. I've tried doing the following:
However this attempt to manually update
modified_by
does not work. I assumed this would be becausemodified_by
is aLastUserField
which has a default ofeditable=False
, but even after creating my ownEditableAuthStampedModel
where themodified_by
field is set to aLastUserField
with an explicit value ofeditable=True
, themodified_by
value that I manually set does not get saved.Any ideas?
The text was updated successfully, but these errors were encountered: