reading-notes

This Repo required for Asac labs class 2


Project maintained by ManarAbdelkarim Hosted on GitHub Pages — Theme by mattgraham

Django Custom User

sometimes we don’t want to use only the feilds available in the built in user model or we want to change some feilds , for example we may want to make the user log in by using email instead of the username

in today’s read I will walk with you in the steps of how to create a custom user model

  1. create an app and add it in the installed apps in the settings
  1. we will start bulding our custom model inside the app “account” model
  1. create a new file in the accounts app called forms.py

     touch account/forms.py
    
  2. We’ll update it with the following code to largely subclass the existing forms.

     from django import forms
     from django.contrib.auth.forms import UserCreationForm, UserChangeForm
     from .models import CustomUser
    
     class CustomUserCreationForm(UserCreationForm):
    
         class Meta:
             model = CustomUser
             fields = ('username', 'email')
    
     class CustomUserChangeForm(UserChangeForm):
    
         class Meta:
             model = CustomUser
             fields = ('username', 'email')
    
  3. Finally we update admin.py since the Admin is highly coupled to the default User model.

     # accounts/admin.py
     from django.contrib import admin
     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)