Search code examples
pythondjangodjango-modelsdjango-signals

Signal for the default user registration of django


I would like after creating a user to run a method and create content in other tables of this user that was created but I do not have a user model since I use the default model for the record.

Problems: -I do not have user model (use the default model) -I want to choose the newly created user to create the other records in other tables -I'm not an expert in django (I'm just trying to solve a problem in another area and this would help)

I like the way to do it in this post:

https://simpleisbetterthancomplex.com/tutorial/2016/07/28/how-to-create-django-signals.html

Creating a signals.py to control the signals


Solution

  • You can attach a Django signal to the Django default User models with no problems, for example:

    @receiver(post_save, sender=User)
    def save_user_profile(sender, instance, **kwargs):
        instance.profile.save()
    

    The instance argument is the new user recently created into the database, so you can use it as a reference to to add the new tables.

    As well, you can put this function in your models.py as well, not necessarily need to create a signals.py