#27462: Attribute "pk_set" is always is not empty, even m2m is not changed at 
all.
-----------------------------------------+------------------------
               Reporter:  Seti           |          Owner:  nobody
                   Type:  Uncategorized  |         Status:  new
              Component:  Documentation  |        Version:  1.10
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 A code of signal;

 {{{

 @receiver(m2m_changed, sender=Group.user_set.through)
 def changed_m2m(sender, instance, action, reverse, model, pk_set,
 **kwargs):

     print('Signal "{}" | Users in group: {} | pk_set: {}'.format(action,
 instance.user_set.all(), pk_set))

 }}}


 Run code:


 {{{
         g1 = Group.objects.all()[0]

         g1.user_set.add(UserModel.objects.all()[0])
         g1.user_set.add(UserModel.objects.all()[0])
         g1.user_set.add(UserModel.objects.all()[0])

         g1.user_set.remove(UserModel.objects.all()[0])
         g1.user_set.remove(UserModel.objects.all()[0])
         g1.user_set.remove(UserModel.objects.all()[0])
 }}}


 Results:

 {{{
 Signal "pre_add" | Users in group: <QuerySet []> | pk_set: {UUID
 ('d0d4a1eb-d2c0-4a27-b9a6-a057a4fa25bc')}
 Signal "post_add" | Users in group: <QuerySet [<User: Martin Rivera>]> |
 pk_set: {UUID('d0d4a1eb-d2c0-4a27-b9a6-a057a4fa25bc')}
 Signal "pre_add" | Users in group: <QuerySet [<User: Martin Rivera>]> |
 pk_set: set()
 Signal "post_add" | Users in group: <QuerySet [<User: Martin Rivera>]> |
 pk_set: set()
 Signal "pre_add" | Users in group: <QuerySet [<User: Martin Rivera>]> |
 pk_set: set()
 Signal "post_add" | Users in group: <QuerySet [<User: Martin Rivera>]> |
 pk_set: set()
 Signal "pre_remove" | Users in group: <QuerySet [<User: Martin Rivera>]> |
 pk_set: {UUID('d0d4a1eb-d2c0-4a27-b9a6-a057a4fa25bc')}
 Signal "post_remove" | Users in group: <QuerySet []> | pk_set: {UUID
 ('d0d4a1eb-d2c0-4a27-b9a6-a057a4fa25bc')}
 Signal "pre_remove" | Users in group: <QuerySet []> | pk_set: {UUID
 ('d0d4a1eb-d2c0-4a27-b9a6-a057a4fa25bc')}
 Signal "post_remove" | Users in group: <QuerySet []> | pk_set: {UUID
 ('d0d4a1eb-d2c0-4a27-b9a6-a057a4fa25bc')}
 Signal "pre_remove" | Users in group: <QuerySet []> | pk_set: {UUID
 ('d0d4a1eb-d2c0-4a27-b9a6-a057a4fa25bc')}
 Signal "post_remove" | Users in group: <QuerySet []> | pk_set: {UUID
 ('d0d4a1eb-d2c0-4a27-b9a6-a057a4fa25bc')}

 }}}


 If action == 'pre_remove', then pk_set has primary keys of objects which
 is not presets in a queryset of other model. It is an expected behaviour.

 But if action == 'post_remove' then pk_set **always** contains primary
 keys of objects, even queryset is empty. Does is expected behaviour?

 The Django`s docs says next:
 https://docs.djangoproject.com/en/1.10/ref/signals/#m2m-changed

 ''pk_set
 For the pre_add, post_add, pre_remove and post_remove actions, this is a
 set of primary key values that have been added to or removed from the
 relation.''

 So, why the pk_set for the action == 'post_remove' always not empty?


 I tested it on the Django 1.10 and the 1.9,  the result is same.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/056.81d06b960d5aee5f10122b8e2c20d2d0%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to