Django has the default user model which comes with some of the useful fields. You should embrace using these fields in your project.
Here is the list of default Django user fields.
# | User Model Field | Data Type | Description |
---|---|---|---|
1 | _state | ModelState | It is used to preserve the state of the user. |
2 | id | Number | Unique ID for each user. |
3 | password | string | Encrypted password for the user. |
4 | last_login | datetime | Date and time when user logged in last time. |
5 | is_superuser | bool | True if the user is superuser, otherwise false. |
6 | username | string | Unique username for the user. |
7 | first_name | string | First name of the user. |
8 | last_name | string | Last name of the user. |
9 | Email ID of the user. | ||
10 | is_staff | bool | Set true if the user is a staff member, else false. |
11 | is_active | bool | Is profile active. |
12 | date_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. |
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.
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.