Search code examples
djangodjango-signals

how to group different model updates when listening to post_save in changes


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?


Solution

  • 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