Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

VivekAsCoder
VivekAsCoder

Posted on

     

Simple Custom User Model In Django :: CookBook

Custom User Model in Django.

Steps Involve.

  • Creating custom user model and manager.
  • Update the settings.py
  • Customize theUserCreationForm 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
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

Settings.py

AUTH_USER_MODEL='app.CustomUser'
Enter fullscreen modeExit fullscreen mode

Forms.py

fromdjango.contrib.auth.formsimportUserCreationForm,UserChangeFormfromapp.modelsimportCustomUserclassCustomUserCreationForm(UserCreationForm):classMeta(UserCreationForm):model=CustomUserfields=('phone_no',)classCustomUserChangeForm(UserChangeForm):classMeta:model=CustomUserfields=('phone_no',)
Enter fullscreen modeExit fullscreen mode

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)
Enter fullscreen modeExit fullscreen mode

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
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
gravesli profile image
gravesli
Developer who has a passion for Python.
  • Email
  • Location
    Boston
  • Work
    Software 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~

CollapseExpand
 
gravesli profile image
gravesli
Developer who has a passion for Python.
  • Email
  • Location
    Boston
  • Work
    Software Support Engineer
  • Joined

Hi, thanks for your reply. Would you give me a star on GitHub? because my project isn't active. ^.^

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

403 Bio is forbidden.
  • Joined

More fromVivekAsCoder

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp