Sequence of arguments in django signals

Viewed 325

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)

1 Answers

From Django documentation, arguments are :

  • sender : The model class.
  • instance : The actual instance being saved.
  • created : A boolean; True if a new record was created.
  • raw : A boolean; True if the model is saved exactly as presented (i.e. when loading a fixture). One should not query/modify other records in the database as the database might not be in a consistent state yet.
  • using : The database alias being used.
  • update_fields : The set of fields to update as passed to Model.save(), or None if update_fields wasn’t passed to save()

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,
    )

...
Related