1、在app下创建一个自己用户认证文件,文件名随意,记得为.py文件

自定义用户认证(继承django的)

 

2、编辑该userauth.py文件

  1 #!/usr/bin/env python
  2 #coding:utf-8
  3 from django.db import models
  4 from django.contrib.auth.models import (
  5     BaseUserManager, AbstractBaseUser
  6 )
  7 import django
  8 
  9 class UserManager(BaseUserManager):
 10     def create_user(self, email, name, password=None):
 11         """
 12         Creates and saves a User with the given email, date of
 13         birth and password.
 14         """
 15         if not email:
 16             raise ValueError('Users must have an email address')
 17 
 18         user = self.model(
 19             email=self.normalize_email(email),
 20             name=name,
 21             #token=token,
 22             #department=department,
 23             #tel=tel,
 24             #memo=memo,
 25 
 26         )
 27 
 28         user.set_password(password)
 29         user.save(using=self._db)
 30         return user
 31 
 32     def create_superuser(self, email, name ,password):
 33         """
 34         Creates and saves a superuser with the given email, date of
 35         birth and password.
 36         """
 37         user = self.create_user(email,
 38             password=password,
 39             name=name,
 40             #token=token,
 41             #department=department,
 42             #tel=tel,
 43             #memo=memo,
 44         )
 45         user.is_admin = True
 46         user.save(using=self._db)
 47         return user
 48 
 49 
 50 class UserProfile(AbstractBaseUser):
 51     email = models.EmailField(
 52         verbose_name='email address',
 53         max_length=255,
 54         unique=True,
 55     )
 56 
 57     is_active = models.BooleanField(default=True)
 58     is_admin = models.BooleanField(default=False)
 59 
 60     name = models.CharField(u'名字', max_length=32)
 61     token = models.CharField(u'token', max_length=128,default=None,blank=True,null=True)
 62     department = models.CharField(u'部门', max_length=32,default=None,blank=True,null=True)
 63 
 64 
 65     mobile = models.CharField(u'手机', max_length=32,default=None,blank=True,null=True)
 66 
 67     memo = models.TextField(u'备注', blank=True,null=True,default=None)
 68     date_joined = models.DateTimeField(blank=True, auto_now_add=True)
 69     valid_begin_time = models.DateTimeField(default=django.utils.timezone.now)
 70     valid_end_time = models.DateTimeField(blank=True,null=True)
 71 
 72 
 73 
 74 
 75 
 76     USERNAME_FIELD = 'email'  #定义email为用户名
 77     #REQUIRED_FIELDS = ['name','token','department','tel','mobile','memo']
 78     REQUIRED_FIELDS = ['name']
 79 
 80     def get_full_name(self):
 81         # The user is identified by their email address
 82         return self.email
 83 
 84     def get_short_name(self):
 85         # The user is identified by their email address
 86         return self.email
 87 
 88     def __str__(self):              # __unicode__ on Python 2
 89         return self.email
 90 
 91     def has_perm(self, perm, obj=None):
 92         "Does the user have a specific permission?"
 93         # Simplest possible answer: Yes, always
 94         return True
 95     def has_perms(self, perm, obj=None):
 96         "Does the user have a specific permission?"
 97         # Simplest possible answer: Yes, always
 98         return True
 99     def has_module_perms(self, app_label):
100         "Does the user have permissions to view the app `app_label`?"
101         # Simplest possible answer: Yes, always
102         return True
103 
104     @property
105     def is_staff(self):
106         "Is the user a member of staff?"
107         # Simplest possible answer: All admins are staff
108         return self.is_admin
109 
110     class Meta:
111         verbose_name = u'用户信息'
112         verbose_name_plural = u"用户信息"
113     def __unicode__(self):
114         return self.name
115 
116     objects = UserManager()
View Code

相关文章:

  • 2021-10-08
  • 2022-12-23
  • 2022-12-23
  • 2022-02-14
  • 2021-12-14
  • 2021-11-01
  • 2021-11-02
  • 2022-12-23
猜你喜欢
  • 2018-03-07
  • 2021-07-11
  • 2021-10-30
  • 2021-06-04
  • 2022-12-23
  • 2021-12-02
  • 2021-10-26
相关资源
相似解决方案