Created
November 2, 2018 19:23
-
-
Save wsvincent/8c9ba957c29d35faa65d15a89c88600c 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
One thing that really hung me up was why my custom fields like 'photo' or 'relationship' wasn't showing in admin. After some searching I found something like the below to resolve the issue.
sources: https://stackoverflow.com/questions/15012235/using-django-auth-useradmin-for-a-custom-user-model, https://stackoverflow.com/questions/1437991/django-admin-fieldsets/1485005
class CustomUserAdmin(UserAdmin): add_form = CustomUserCreationForm form = CustomUserChangeForm model = CustomUser list_display = ["username", "email", "photo"] # Fieldsets fieldsets = UserAdmin.fieldsets + ( ("Profile Image", { "fields": ("photo",) }), ("Relationships", { "fields": ("relationship",) }), ) add_fieldsets = ( (None, {"classes": ("wide",), "fields": ("username", "email", "photo")}), ) admin.site.register(CustomUser, CustomUserAdmin)