12 Django Default User Model Fields You Should Know

12 Django Default User Model Fields You Should Know

Django has the default user model which comes with some of the useful fields. You should embrace using these fields in your project.

Django Default User Model Fields

Here is the list of default Django user fields.

#User Model FieldData TypeDescription
1_stateModelStateIt is used to preserve the state of the user.
2idNumberUnique ID for each user.
3password stringEncrypted password for the user.
4last_logindatetimeDate and time when user logged in last time.
5is_superuserboolTrue if the user is superuser, otherwise false.
6usernamestringUnique username for the user.
7first_name stringFirst name of the user.
8last_name stringLast name of the user.
9emailemailEmail ID of the user.
10is_staff boolSet true if the user is a staff member, else false.
11is_active boolIs profile active.
12date_joined datetime Date and time when the user joined the first time. It is usually when the user signs up or creates a user account the first time.

How to read all the user model fields?

As this is the default model, you don’t need to define it anywhere. You can simply use it.

Reading model data in Django is pretty easy.

Import User model from the set of Django authentication models.

from django.contrib.auth.models import User

users = User.objects.filter()

Just to see or to debug you can also print all the user fields for a particular user. Let’s say print all the fields for the first user.

from django.contrib.auth.models import User

users = User.objects.filter()

print(users[0].__dict__)

These user-defined fields are also used for user authentication in the Django registrations.

You can also check all the user’s data on the Django admin site.

Django default user model fields

Extending Django User Model

There are some more fields you might need for your user profile in your project. Let’s say, you also want to save the mobile number of the user. Likewise, there can be many fields you want to add.

For this, you can extend the Django Default User Model Fields. It is like creating a new model (says profile) and linking with the default User model. The relationship between the two models will be OneToOne-mapping. We will see that in the next tutorial.

Any doubt? Let’s discuss this in the comment section below.

Leave a Reply

Your email address will not be published. Required fields are marked *