#30287 closed Uncategorized (duplicate)
post_remove and post_add behave differently for m2m_changed
Reported by: | nirmalraghavan | Owned by: | nobody |
---|---|---|---|
Component: | Uncategorized | Version: | 2.1 |
Severity: | Normal | Keywords: | signals |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Consider the following Model,
class MyModel(models.Model): likes = models.ForeignKey(User)
Now if I add a user to the likes field as below,
myObj.likes.add(user)
my m2m_changed receiver method will be called in signals module with action post_add and pk_set as the User ID. If I try to add likes again with the same user and same above method, now post_add again called with pk_set as empty.
And when I try to remove the user as below,
myObj.likes.remove(user)
m2m_changed receiver method is called with action post_remove and pk_set as User ID. And if I try to remove the same user again, this time also m2m_changed receiver method is called. But this time pk_set contains the User ID.
For post_add, pk_set will be empty after first add. Subsequent calls will always set pk_set as empty. But for post_remove pk_set will always contains pk_set value. Why it is that?
I'm calculating points based on this like events. Now I need to make sure ManyToMany relationship exist before adding/substracting points by making an extra call to database. If somehow we could know if the relation already added/removed from m2m_changed signal, it will be great.
Change History (1)
comment:1 by , 6 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
https://code.djangoproject.com/ticket/29615