![]() |
VOOZH | about |
Every Django project should implement a custom user model from the start. This approach avoids future issues and allows flexibility in authentication, user fields, and business logic. Two common ways to create a custom user model are: extending "AbstractUser" and using "AbstractBaseUser".
AbstractUser allows you to extend Django’s default User model while retaining its built-in authentication features. Consider a project named "geeksforgeeks" with an app named "geeks", and add "geeks" to INSTALLED_APPS in settings.py.
In geeks/models.py:
In setting.py:
AUTH_USER_MODEL = 'geeks.User'
In geeks/admin.py:
python manage.py makemigrations geeks
python manage.py migrate
python manage.py createsuperuser
Email: admin@example.com
Password: ****
python manage.py runserver
Visit: "http://127.0.0.1:8000/admin/" to manage users.
AbstractBaseUser gives full control over the user model but requires implementing authentication logic, manager classes, and additional fields.
In geeks/models.py:
In settings.py:
AUTH_USER_MODEL = 'geeks.User'
In geeks/admin.py:
python manage.py makemigrations geeks
python manage.py migrate
$ python manage.py createsuperuser
Email: admin@example.com
Password: ****
$ python manage.py runserver
Visit: "http://127.0.0.1:8000/admin/" to manage users.
| AbstractUser | AbstractBaseUser |
|---|---|
| Extends Django's default User with extra fields | Minimal base class, all fields must be defined manually |
| Easy to implement and use | Requires custom manager and full authentication logic |
| Retains username, email, first_name, last_name by default | Only fields you define exist |
| Suitable for small modifications to default User | Suitable for complete control over user model and authentication |