-
-
Save labohkip81/3c0aceeda6e0d712889b6d7aae407aa7 to your computer and use it in GitHub Desktop.
Django Custom User Model for any new project
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
# users/admin.py | |
from django.contrib import admin | |
from django.contrib.auth import get_user_model | |
from django.contrib.auth.admin import UserAdmin | |
from .forms import CustomUserCreationForm, CustomUserChangeForm | |
from .models import CustomUser | |
class CustomUserAdmin(UserAdmin): | |
add_form = CustomUserCreationForm | |
form = CustomUserChangeForm | |
model = CustomUser | |
list_display = ['email', 'username',] | |
admin.site.register(CustomUser, CustomUserAdmin) |
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
# users/forms.py | |
from django import forms | |
from django.contrib.auth.forms import UserCreationForm, UserChangeForm | |
from .models import CustomUser | |
class CustomUserCreationForm(UserCreationForm): | |
class Meta(UserCreationForm): | |
model = CustomUser | |
fields = ('username', 'email') | |
class CustomUserChangeForm(UserChangeForm): | |
class Meta: | |
model = CustomUser | |
fields = ('username', 'email') |
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
# users/models.py | |
from django.contrib.auth.models import AbstractUser | |
from django.db import models | |
class CustomUser(AbstractUser): | |
# add additional fields in here |
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
1. Start a new Django project: `django-admin startproject new_project .` | |
2. Create a `users` app: `python manage.py startapp users` | |
3. Update `new_project/settings.py`, `users/models.py`, `users/forms.py`, `users/admin.py` | |
4. `python manage.py makemigrations users` | |
5. `python manage.py migrate` |
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
# new_project/settings.py | |
INSTALLED_APPS = [ | |
'django.contrib.admin', | |
'django.contrib.auth', | |
'django.contrib.contenttypes', | |
'django.contrib.sessions', | |
'django.contrib.messages', | |
'django.contrib.staticfiles', | |
# Local | |
'users.apps.UsersConfig', # new | |
] | |
... | |
AUTH_USER_MODEL = 'users.CustomUser' # new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment