Opened 6 years ago

Closed 6 years ago

#30571 closed Cleanup/optimization (invalid)

Incorrect value of kwargs in pre_init signal documentation.

Reported by: Pratyush Singh Owned by: Hasan Ramezani
Component: Documentation Version: 2.2
Severity: Normal Keywords: signals
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I was working on a follower-following system for users.

Model -

class Follower(models.Model):
    follower = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='following')
    following = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='followers')
    follow_time = models.DateTimeField(auto_now=True)

    class Meta:
        unique_together = ('follower', 'following')

I created this pre_init signal (to prevent a user from following self, but details excluded)-

@receiver(signals.pre_init, sender=user_models.Follower)
def prevent_user_following_self(sender, *args, **kwargs):
    print(kwargs)

After this, I tried the following in Django shell -

>>> import django
>>> django.__version__
'2.2.2'
>>> from django.contrib.auth.models import User
>>> from users.models import Follower
>>> a = User.objects.get(pk=1)
>>> Follower(follower=a, following=a)
{'signal': <django.db.models.signals.ModelSignal object at 0x7f431561dc88>, 'args': (), 'kwargs': {'follower': <User: admin>, 'following': <User: admin>}}

Here, kwargs contains the actual kwargs under a nested name. While according to the documentation, the print here should give

{'kwargs': {'follower': <User: admin>, 'following': <User: admin>}

Change History (4)

comment:1 by Mariusz Felisiak, 6 years ago

Component: Database layer (models, ORM)Documentation
Easy pickings: set
Summary: Incorrect value of kwargs in pre_init signalIncorrect value of kwargs in pre_init signal documentation.
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Thanks for the report. Yes it should be clarified in an example and kwargs description.

comment:2 by Hasan Ramezani, 6 years ago

Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:3 by Hasan Ramezani, 6 years ago

Has patch: set
Version 0, edited 6 years ago by Hasan Ramezani (next)

comment:4 by Mariusz Felisiak, 6 years ago

Resolution: invalid
Status: assignedclosed

Ahh, sorry but I just realized that everything works properly. If you will change your method signature, to the

@receiver(signals.pre_init, sender=user_models.Follower)
def prevent_user_following_self(sender, args, kwargs, **other):
    print(kwargs)
    print(args)

then you will get args and kwargs as described in documentation.

Note: See TracTickets for help on using tickets.
Back to Top