Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
User Model | Advanced
Django ORM Ninja: Técnicas Avanzadas para Desarrolladores
course content

Contenido del Curso

Django ORM Ninja: Técnicas Avanzadas para Desarrolladores

Django ORM Ninja: Técnicas Avanzadas para Desarrolladores

1. Introduction to Django ORM
2. Models, datatypes, and fields
3. Queries
4. Relations
5. Complex Queries
6. Advanced

bookUser Model

Have you ever wondered how to implement authentication on your website so that not every user has the same permissions and limited functionality? Well, let's start with the topic of User Model.

Django provides a robust and versatile built-in User Model, which is an integral component of Django's authentication system. This model is designed to handle user accounts, providing essential fields and behaviors for user authentication and management. It's a ready-to-use, database-driven model that simplifies the process of handling user data, making it an efficient starting point for most web applications.

The default User Model in Django comes with a variety of fields that are common to web applications. These include:

  • username: A unique identifier for the user.
  • password: Stored in a hashed format for security.
  • Email Address: Optionally used for user identification and communication.
  • First and Last Name: For storing the user's full name.
  • is_active: A boolean indicating if the user account is active.
  • is_staff: A boolean indicating if the user has access to the admin site.
  • is_superuser: A boolean indicating if the user has all permissions without explicitly assigning them.

We have already encountered the User Model when we created a superuser for accessing the admin panel. We filled in the mandatory fields: username, email, and password. All other fields are not mandatory.

Extend the Default User Model

Sometimes you may need to store additional information about users that isn't covered by the default fields, like a profile picture, bio, or contact details or if you want to authenticate users based on email addresses or phone numbers instead of usernames, you’ll need to extend the User Model.

When extending the User Model, it's important to decide whether to subclass AbstractUser (which keeps all the fields of the default User Model and allows adding new ones) or AbstractBaseUser (which requires you to define all fields explicitly, offering maximum customization). The choice depends on how much deviation you need from the standard model.

Let's redesign the Author model into a User Model with an additional field, pen_name, and add authentication via email instead of the username.

We can remove all other fields like first_name and last_name, as they are inherited from the AbstractUser model.

In settings.py, you should specify your new user model:

Where app is our app name, and Author is our model that is inherited from the AbstractUser model.

Also, we need to slightly modify the code in the admin.py file and re-register the Author model, as it is not just a model but an AbstractUser. Other codes must remain the same.

In most cases, this code is simply copied and slightly modified depending on which fields you need to add. Now, each author can register themselves on our site and have the ability to add, delete, and update books, in addition to standard viewing.

1. What is the difference between AbstractUser and AbstractBaseUser in Django?
2. Why might a developer choose to extend Django’s User model?
3. How can you modify the Django User model to use email as the username?
What is the difference between AbstractUser and AbstractBaseUser in Django?

What is the difference between AbstractUser and AbstractBaseUser in Django?

Selecciona la respuesta correcta

Why might a developer choose to extend Django’s User model?

Why might a developer choose to extend Django’s User model?

Selecciona la respuesta correcta

How can you modify the Django User model to use email as the username?

How can you modify the Django User model to use email as the username?

Selecciona la respuesta correcta

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 6. Capítulo 4
some-alt