Creating a User Profile model in Django to hold additional user info
Creating a new user in a Django project is quite straightforward and well-documented.
But most of the time, you want to capture more data than the basic user model provided by Django. You see, the default Django user model only contains fields for the first name, last name, and email. The typical registration flow usually requires more data to be captured. In this case, the recommended way is to create a user profile model, and "link" it to the user using a one-to-one relationship.
In this post, we will show a complete example of declaring a user profile model, the registration form that contains both the user fields and the user profile fields, and finally the view that will handle the creation of the user and user profile instances in the database.
Model
The only highlight in this model declaration is the use of OneToOneField
relationships. This means that instances of this UserProfile
model can point to one and only one instance of the User
model.
Also, note that we are not referencing directly the User
model, but we are using settings.AUTH_USER_MODEL
as recommended in the documentation to support custom-defined models. Finally, we are using on_delete=models.CASCADE
to ensure that when a User
is deleted, the "linked" UserProfile
will be deleted as well.
Form
This is the form used to present and validate the registration page. Notice that the first 4 fields refer to the User
model, and the rest of them in the UserProfile
model.
View
This is a Class-Based View (CBV) that is specifically for rendering and saving models based on a provided form (notice that it's extending FormView
class).
The main work of this view takes place in the form_valid()
method. This is called when the elements in the form are validated and this is called for you to implement the business logic of constructing and saving the models.
Since you will be saving 2 instances, one User
and one UserProfile
, notice that we are using the @transaction.atomic
annotation to ensure that if for some reason one of the model creation fails, the database will roll back the changes. (read this for more details on Django and atomic transactions).
The rest of the code is just for first creating a User
using the create_user()
method and then a UserProfile
that references the newly created User
.
Hopefully, this was a quick and to-the-point tutorial on how to create a User
with a UserProfile
to hold additional data about your users.
Happy coding!