Django m2m_changed pk_set is empty

69 views
Skip to first unread message

Jason Robinson

unread,
Jun 10, 2017, 5:28:12 PM6/10/17
to Django users
Hi everyone,

Django version 1.10.7, I have a problem where sometimes my receiving `m2m_changed` signal on a ManyToMany field has `pk_set` empty.

The model m2m part (note it's a `self` reference):

    class Profile(TimeStampedModel):
        following
= models.ManyToManyField(
           
"self", verbose_name=_("Following"), related_name="followers"
       
)


Signal beginning part with logger:
   
    @receiver(m2m_changed, sender=Profile.following.through)
   
def profile_following_change(sender, instance, action, pk_set, **kwargs):
        logger
.debug("profile_following_change - sender %s, instance %s, action %s, pk_set %s, kwargs: %s",
                     sender
, instance, action, pk_set, kwargs)


Below an example log line with `pk_set` containing the added primary key:

    DEBUG:socialhome:profile_following_change -
        sender
<class 'socialhome.users.models.Profile_following'>,
        instance
Profile A (profile_a@a.domain.tld),
        action post_add
,
        pk_set
{2},
        kwargs
: {
           
'signal': <django.db.models.signals.ModelSignal object at 0x7fa73e033908>,
           
'model': <class 'socialhome.users.models.Profile'>,
           
'using': 'default',
           
'reverse': False
       
}

    
And an example with an empty `pk_set`.

    DEBUG:socialhome:profile_following_change -
        sender
<class 'socialhome.users.models.Profile_following'>,
        instance
Profile B (profile_b@b.domain.tld),
        action post_add
,
        pk_set
set(),
        kwargs
: {
           
'model': <class 'socialhome.users.models.Profile'>,
           
'reverse': False,
           
'using': 'default',
           
'signal': <django.db.models.signals.ModelSignal object at 0x7f001c173908>
       
}


    
The two saves are both from the same code, using the following line. Note this code runs in an RQ background process, should that matter:

    profile.following.add(user.profile)



Why is the `pk_set` sometimes empty, any ideas? Note that both `Profile` objects also exist before the RQ job is processed (event is triggered by doing a "follow" in the UI).

Also of note, added object *is* found in the ManyToMany field after the operation.

Thankful of any ideas,

Br,
Jason Robinson
https://jasonrobinson.me

Tim Graham

unread,
Jun 14, 2017, 1:16:18 PM6/14/17
to Django users
Reply all
Reply to author
Forward
0 new messages