How to Extend the User Model in Django



Python


In this article, we show how to extend the User model in Django.

We would extend the User model if we want to add our own custom attributes to each User on the site that isn't part of a Django's built-in User model.

With Django's built-in User model, there are username, password, email, first_name, last_name, and is_staff attributes.

However, if you want to have other attributes, such as maybe city or state, or userid (if you want each user to have a unique identifier other than the primary key), then you can either extend the Django model or build a custom User model.

In this article, we go the route of extending the User model so that we can add more attributes that aren't present in the default Django model. We will add city, state, and a userid attribute to each User.

The advantage of extending the User model instead of just creating a separate independent database to store user information is that on any page in our website, we can access information from the User model (plain or extended), using request.user attributes. Doing this with a separate, independent database would be complex and complicated, because then we would have to import onto every page of our website the model which has the User information. Extending the User model or creating your own custom User model is much easier and more effective and efficient for our website. Using request.user attributes, we easily have any User information at our disposal on any page without needing import data.

So to extend the User model, we have the following code shown below.

We will create a model called UserAdditionalInfo and create the fields, city and state.

We will then show how we can access these attributes on any page on our website.



We then have to make sure we run, py manage.py makemigrations, followed by, py manage.py migrate.

In this code, we create a model called UserAdditionalInfo.

We create a field, user, and make a OnetoOneField with settings.AUTH_USER_MODEL

What this line of code does is that only one UserAdditionalInfo model can exist for each user. So the UserAdditionalInfo is unique to the User model. They have a one-to-one relationship. So the city and state attributes in the UserAdditionalInfo model are unique to the user. The UserAdditionalInfo Model can only have one User object and the User model can only have one UserAdditionalInfo object. nvn

We create a __str__ method just to make it easier to read the city and state of the user in admin.

After this, we define a function, post_save_user_model_receiver.

What this function is going to do is, once the User model is created, it will send a signal, so that the UserAdditionalInfo model is created.

So it takes in a few parameters.

The sender will be the User model. The User model will send a signal (to create the UserAdditional Model once the User model is created).

instance can be used to check to see if the current user is in the

created can be used to check to see if the User model has been created.

The statement, if created:, checks to see if the User model has been created.

If the User model has been created, we try to to create the UserAdditionalInfo model for this user.

This block simply creates the function.

Now we have to run the function.

We runt he function with the statement, post_save.connect(post_save_user_model_receiver, sender= settings.AUTH_USER_MODEL)

post_save_user_model is the receiver and the sender is settings.AUTH_USER_MODEL

So we've now extended the User model in Django.

Now we can use request.user attributes to display information about the user.

Now in order to output the attributes from the UserAdditionalInfo model, we can use the statements below.



Because the UserAdditional model extends the User model, we can now reference the data from the UserAdditional model using request.user attributes.

And this is how we can extend the User model in Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...