Custom User Model in Django.
Steps Involve.
- Creating custom user model and manager.
- Update the settings.py
- Customize the
UserCreationForm
andUserChangeForm
. - Finally update the admin.py file for custom user model.
Models.py
fromdjango.dbimportmodelsfromdjango.contrib.auth.modelsimportAbstractBaseUserfromdjango.contrib.auth.modelsimportPermissionsMixinfromdjango.utils.translationimportgettext_lazyas_fromdjango.utilsimporttimezonefromapp.managersimportCustomUserManagerclassCustomUser(AbstractBaseUser,PermissionsMixin):phone_no=models.CharField(_("Phone Number"),unique=True,max_length=10)is_staff=models.BooleanField(default=False)is_active=models.BooleanField(default=False)date_joined=models.DateTimeField(default=timezone.now)USERNAME_FIELD='phone_no'REQUIRED_FIELDS=[]objects=CustomUserManager()def__str__(self):returnself.phone_no
Manager.py
fromdjango.contrib.auth.base_userimportBaseUserManagerfromdjango.utils.translationimportugettext_lazyas_classCustomUserManager(BaseUserManager):defcreate_user(self,phone_no,password,**extra_fields):ifnotphone_no:raiseValueError(_("The phone no. must be provided."))iflen(phone_no)!=10:raiseValueError(_("The Phone No Should be 10 digits long."))user=self.model(phone_no=phone_no,**extra_fields)user.set_password(password)user.save()returnuserdefcreate_superuser(self,phone_no,password,**extra_fields):extra_fields.setdefault('is_staff',True)extra_fields.setdefault('is_superuser',True)extra_fields.setdefault('is_active',True)ifextra_fields.get('is_staff')isnotTrue:raiseValueError(_('Superuser must have is_staff=True'))ifextra_fields.get('is_superuser')isnotTrue:raiseValueError(_('Superuser must have is_superuser=True'))returnself.create_user(phone_no,password,**extra_fields)
Settings.py
AUTH_USER_MODEL='app.CustomUser'
Forms.py
fromdjango.contrib.auth.formsimportUserCreationForm,UserChangeFormfromapp.modelsimportCustomUserclassCustomUserCreationForm(UserCreationForm):classMeta(UserCreationForm):model=CustomUserfields=('phone_no',)classCustomUserChangeForm(UserChangeForm):classMeta:model=CustomUserfields=('phone_no',)
Admin.py
fromdjango.contribimportadminfromdjango.contrib.auth.adminimportUserAdminfrom.formsimportCustomUserChangeForm,CustomUserCreationFormfrom.modelsimportCustomUserclassCustomUserAdmin(UserAdmin):add_form=CustomUserCreationFormform=CustomUserChangeFormmodel=CustomUserlist_display=('phone_no','is_staff','is_active')list_filter=('phone_no','is_staff','is_active')fieldsets=((None,{'fields':('phone_no','password')}),('Permissions',{'fields':('is_staff','is_active')}),)add_fieldsets=((None,{'classes':('wide',),'fields':('phone_no','password1','password2','is_staff','is_active')}),)search_fields=('phone_no',)ordering=('phone_no',)admin.site.register(CustomUser,CustomUserAdmin)
Quick Note:
I wanted to make user model as simple as possible so this is what i did, create a custom user model with basically only two fieldsphone_no
andpassword
.
Top comments(2)
Subscribe

gravesli•
Developer who has a passion for Python.
- Email
- LocationBoston
- WorkSoftware Support Engineer
- Joined
I think you are great! i just want to discuss tech with Python developer.
I built a display machine state using Python3 with Flask!
Flask State Github:github.com/yoobool/flask-state
Should i can get some improvement suggestions from you? Thanks~

gravesli•
Developer who has a passion for Python.
- Email
- LocationBoston
- WorkSoftware Support Engineer
- Joined
Hi, thanks for your reply. Would you give me a star on GitHub? because my project isn't active. ^.^
For further actions, you may consider blocking this person and/orreporting abuse