djangoargumentssignalssequencereceiver

Sequence of arguments in django signals



Below is the receiver function that I wrote.
@receiver(post_save,sender=User)
def create_profile(sender, instance, created, **kwargs):   
    if created:
        Profile.objects.create(user=instance)

Is there a specific sequence for the arguments in this function?
Can I change the positioning of these arguments?
Eg - def create_profile(sender, created, instance, **kwargs)


Solution

  • From Django documentation, arguments are :

    Every argument is sent as a keyword argument, so you can use them in any order as long as you give them a default value.

    For instance :

    @receiver(post_save,sender=User)
    def create_profile(raw=True, sender=None, created=False, **kwargs):
        ...
    

    You can see how the argument are provided by looking at where post_save is sent :

    ...
    
    # Signal that the save is complete
    if not meta.auto_created:
        post_save.send(
            sender=origin, instance=self, created=(not updated),
            update_fields=update_fields, raw=raw, using=using,
        )
    
    ...