Role of models.Model

The most basic model class in Django is models.Model. Every Django model is defined by inheriting from models.Model. This class maps the model I define to a database table. Each model class represents a table in the database, and the fields within that model become the columns of that table.

An important point is that by inheriting from models.Model, several useful methods are provided for basic use:

  • save(): A method to save an object to the database
  • delete(): A method to delete an object
  • objects.create(): A method to create and save a new object
  • objects.filter(): A method to retrieve objects that match certain conditions

Thanks to these functionalities, simply inheriting from models.Model allows developers to easily use the powerful features of Django ORM.

Relationship Between AbstractUser and CustomUser

Django comes with a built-in user model called auth.User. While this model can be used out of the box, there are many cases where developers want to customize the user model. For instance, there may be a need to add additional fields like phone numbers or addresses.

In such cases, AbstractUser is used. This class is an abstract class designed to extend the default user model provided by Django (i.e., auth.User). Since AbstractUser also inherits from models.Model, if I inherit from AbstractUser to define CustomUser, I can still use all the features of the ORM.

from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    phone_number = models.CharField(max_length=15, blank=True)
    address = models.CharField(max_length=255, blank=True)

The defined CustomUser model provides all functionalities related to user authentication while allowing for the definition of additional fields freely.

Role of models.Manager and AbstractUserManager

In Django models, models.Manager acts as an interface for handling queries to the database. With the default objects manager, developers can easily retrieve or manipulate data. For example, there are methods like filter(), get(), and create().

When the user model requires user creation logic, AbstractUserManager can be used. AbstractUserManager inherits from models.Manager and provides specialized methods for user creation (create_user(), create_superuser()).

from django.contrib.auth.models import BaseUserManager

class CustomUserManager(BaseUserManager):
    def create_user(self, email, password=None, **extra_fields):
        # User creation logic
        pass

    def create_superuser(self, email, password=None, **extra_fields):
        # Superuser creation logic
        pass

By customly implementing user creation logic in CustomUserManager, developers can manage the user model more flexibly.

Summary: Relationships Between Model Classes

To summarize the relationships between Django's model classes:

  • models.Model: The base class for all Django models. It maps to the database and provides ORM functionality.
  • AbstractUser: An abstract class that inherits from models.Model. Custom models can be defined by extending this.
  • AbstractUserManager: Inherits from models.Manager and allows customization of user creation logic.

Understanding and utilizing this structure will enable more efficient design of Django's model system.