[Django] #30287: post_remove and post_add behave differently for m2m_changed

15 views
Skip to first unread message

Django

unread,
Mar 25, 2019, 11:59:09 AM3/25/19
to django-...@googlegroups.com
#30287: post_remove and post_add behave differently for m2m_changed
------------------------------------------+-------------------------
Reporter: nirmalraghavan | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 2.1
Severity: Normal | Keywords: signals
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+-------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30287>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 25, 2019, 12:04:47 PM3/25/19
to django-...@googlegroups.com
#30287: post_remove and post_add behave differently for m2m_changed
--------------------------------+--------------------------------------
Reporter: nirmalraghavan | Owner: nobody
Type: Uncategorized | Status: closed
Component: Uncategorized | Version: 2.1
Severity: Normal | Resolution: duplicate

Keywords: signals | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+--------------------------------------
Changes (by nirmalraghavan):

* status: new => closed
* resolution: => duplicate


Comment:

https://code.djangoproject.com/ticket/29615

--
Ticket URL: <https://code.djangoproject.com/ticket/30287#comment:1>

Reply all
Reply to author
Forward
0 new messages