0

I have a bunch of models in my app for which I am listening to changes with post_save. Additionally I have an updated_at field on all of those which is a timestamp that will be updated on every save operation.

Assuming a model Question with a one two many relationship with a model Answer the following happens:

If I add an Answer then post_save will be triggered for both models (because of the timestamp update on Question). In my use case I need to know weather the Question itself was modified (in addition to updated_at) or if post_save was only triggered because an Answer was added.

Is there a clean way to achieve this?

matteok
  • 2,189
  • 3
  • 30
  • 54

1 Answers1

1

Actually I think u can use update_fields argument to check what question fields where modified

@receiver(post_save, sender=Question)
def tri_qu(sender, update_fields, created, instance, **kwargs):
    if not created and update_fields and update_fields == ['updated_at']:
        # ... post_save where triggered, because of answer was added
  • the `post_save` on its own doesn't seem to automatically receive `update_fields` and instead they need to be passed somehow. https://stackoverflow.com/a/55005137/926459 @kingraphaii provides a way to do this in his answer. Combined with your answer this is the solution. – matteok May 02 '19 at 06:12