How to Automatically Create a Database Table Once a User Is Created in Django



Python


In this article, we show how to automatically create a database table once a user is created in Django.

Most websites have users and allow new visitors to sign up.

When a user signs up for your website, you may want to create a variety of database tables (models).

For example, one database table may be User email settings. Another database table may be security settings, such as answers to security questions. We want all of these tables generated right when a user is created.

How do we do this?

We can do this using a post_save signal in Django.

What a post_save signal is it can execute a function after something is executed, in this case, after a user is created.

So in this case, we create a model, UserSetting, after a user is created.

This is done with the following code shown below.



So in the models.py file, we create the model that we want to create once the user is created.

After we create this model, then we create a post_save signal in order to automatically create this model once a user has been created.

So the model we are going to create in this case is UserSetting.

This model will store the user's email settings.

With the row, postemail, a user can decide whether s/he wants to shut off email replies to posts they have created. With the row, commentemail, a user can decide whether s/he wants to shut off email replies to comments they have made. Both are on by default. However, of course, a user can turn off these email settings.

So in order to automatically create this database table once a user is created, we need to create a post_save signal that signals to create this database once a user is created.

We call this post_save signal, post_save_settings_model_receiver

We have to pass in a few parameters into this signal, including sender, instance, created, *args, **kwargs.

Into our post_save signal, we need to have a sender. This is the model that is sending our information. Since Django's built-in User model is sending the signal in this case, it is the sender.

The instance is the instance of the new object we are creating, which is the new object for the UserSetting database talbe.

created can be used, which we do in this case, to determine if a User object has been successfully created. If so, we run the following code.

So if the user was successfully created, we use a try statement to create a new object in the UserSetting model, where the user that was sent is the instance of the user in the UserSetting database table.

We then use the post_save.connect() function to execute our post_save signal. We pass in the post_save signal we've created, along with the sender, which is the User.

Now whenever a user is created, a UserSetting model is created, storing the user's email setting.

And this is how we can automatically create a database table once a user is created in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...