Last active
July 13, 2020 23:24
-
-
Save sadan/683dc54878c2284b335dbd042cd2efca to your computer and use it in GitHub Desktop.
Extending AbstractUser to write a custom User model in Django.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Your admin must also inherit from Django default `UserAdmin`, if you want to save | |
your time from all the hassle of creating some methods they have already created. | |
Now you can edit user admin the way you want by also getting advantage of all the | |
existing admin features. | |
""" | |
from django.contrib.auth import get_user_model | |
from django.contrib.auth.admin import UserAdmin as OrigUserAdmin | |
User = get_user_model() | |
@admin.register(User) | |
class UserAdmin(OrigUserAdmin): | |
list_display = ( | |
'id', 'first_name', 'last_name', 'username', 'email', 'is_active' | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Let's just say we have a ``users`` app in our system and inside the | |
``models.py`` file we write our User model. | |
Also I've added another field for storing cellphone of number of the | |
user. | |
In this example, we are extending Django User model and making email | |
field as our main field for authentication. By default Django uses | |
``username`` field for authentication. In order to do this we have to | |
mark email field as unique and update ``USERNAME_FIELD`` value of the | |
AbstractUser class. | |
This scheme gives us flexibility to use all the exisiting features | |
of Django User model and override what we need, while making sure | |
we don't have to write much code. | |
In similar way, you can also extender or override Django BaseUserManager | |
from ``django.contrib.auth.base_user`` | |
""" | |
from django.contrib.auth.models import AbstractUser | |
from django.db import models | |
from django.utils.translation import gettext_lazy as _ | |
class User(AbstractUser): | |
EMAIL_FIELD = 'email' | |
USERNAME_FIELD = 'email' | |
REQUIRED_FIELDS = ['username'] | |
email = models.EmailField( | |
_('email address'), | |
unique=True, | |
blank=True | |
) | |
cellphone = models.CharField( | |
_('cell phone'), | |
max_length=20, | |
null=True, blank=True | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
In settings file, you have to specify path to the user model you've | |
just created. ``<app_name>.<ModelClassName>`` | |
""" | |
AUTH_USER_MODEL = 'users.User' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment