문제

When the model's ManyToMany field is updated and a m2m_changed signal is sent I am getting every item, previously related and new items, for that relationship in the pk_set argument. Is this the correct behavior? I was expecting only the new item or multiple items that were added to the relationship to be present in the pk_set.

If that is the expected behavior, is there a better way to get only the new items being added to the relationship other than using the post_add signal and comparing the list of items already associated with the model with the incoming list?

Edit: It appears there are no items related to the ManyToMany field in pre_add, then all of the items are there on post_add.

Edit 2: Ok, so the many to many relationship is getting cleared before the list of items is added, receiving the pre_clear and post_clear actions. Not sure how I am supposed to get a diff of how the set of items has changed.

도움이 되었습니까?

해결책

It looks like a better spot to do this comparison is in the model's admin in save_model(). There I can get the original object and the form being applied.

다른 팁

Until Django 1.9, setting objects on an m2m field through direct assignment would call clear and then add.

This is likely the behaviour you were observing. As of Django 1.9, only differences are processed.

If you need the old behaviour, you can pass clear=True to the set method on the related manager.

See https://docs.djangoproject.com/en/dev/releases/1.9/#related-set-direct-assignment

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top