0

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

J.leo
  • 81
  • 9

1 Answers1

0

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

Gregory
  • 6,514
  • 4
  • 28
  • 26
  • Does it work to add it in another model that has nothing to do with users? since I have none to good I will try – J.leo Apr 04 '18 at 15:32
  • Yes, it got to work perfectly. You just do: MyOtherModel.objects.create() – Gregory Apr 05 '18 at 10:36