Django: Getting m2m related objects in signals

后端 未结 1 1653
遥遥无期
遥遥无期 2021-01-24 02:07

I was kind of trying to see similar questions (Django accessing ManyToMany fields from post_save signal), but still don\'t see how to get the updated related objects list.

相关标签:
1条回答
  • 2021-01-24 02:42

    m2m_changed signals are fired at several stages in the save/update process, and there is an action argument supplied to the signal handler that tells you what stage it is in. From the documentation:

    action

    A string indicating the type of update that is done on the relation. This can be one of the following:

    "pre_add" Sent before one or more objects are added to the relation.

    "post_add" Sent after one or more objects are added to the relation.

    "pre_remove" Sent before one or more objects are removed from the relation.

    "post_remove" Sent after one or more objects are removed from the relation.

    "pre_clear" Sent before the relation is cleared.

    "post_clear" Sent after the relation is cleared.

    If you catch the pre_remove action then you will get all the objects before some have been removed from the relationship. That is why you are seeing an apparently unchanged list of users.

    Your code needs to check the action before deciding what to do. For example:

    @receiver(m2m_changed, sender=Blog.users.through)
    def blog_users_change(sender, instance, action, **kwargs):
        if action == 'pre_remove':
             # This will give you the users BEFORE any removals have happened
             print instance.users.all()
        elif action == 'post_remove':
             # This will give you the users AFTER any removals have happened
             print instance.users.all()
    
    0 讨论(0)
提交回复
热议问题