Skip to content

Instantly share code, notes, and snippets.

@wsvincent
Created November 2, 2018 19:23
Show Gist options
  • Save wsvincent/8c9ba957c29d35faa65d15a89c88600c to your computer and use it in GitHub Desktop.
Save wsvincent/8c9ba957c29d35faa65d15a89c88600c to your computer and use it in GitHub Desktop.
Django Custom User Model for any new project
# 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)
# 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')
# users/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
# add additional fields in here
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`
# 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
@mtrazakhan
Copy link

Nice, Can you please make an article related to custom user management:-

  1. create page for user listings with user id, user name, firstname, lastname, email, mobile number, status(active,Inactive), user type(admin, employee), job type(job1, job2, job3), add pagination, filters and search functionality
  2. Create form for Add user :- username, firstname, lastname, email, mobile number, status, type and job role
  3. Edit user
  4. Delete user
    Send random password to email of user after create any user.
    Each user can able to login on same panel with there provided credential.
    Use JWT token instead of session

with templates and users app in navbar

@ilgor
Copy link

ilgor commented Feb 17, 2020

After spending 4 hours I finally made it work. If you follow the Vincent's instructions and you admin page throws error please check your Python version. There was a bug with Python 3.7.0 where /admin/login get stuck in the look referring back to itself. https://code.djangoproject.com/ticket/31067

@BoredVaughn
Copy link

BoredVaughn commented Apr 18, 2020

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment