摘要:自定義一個用戶很簡單然后是最后是這樣一個自定義的用戶模型就弄好了,接下來是自定義登錄字段
自定義一個用戶很簡單models.py
from django.db import models from django.contrib.auth.models import ( BaseUserManager, AbstractBaseUser ) class MyUserManager(BaseUserManager): def create_user(self, email, date_of_birth, password=None): """ Creates and saves a User with the given email, date of birth and password. """ if not email: raise ValueError("Users must have an email address") user = self.model( email=self.normalize_email(email), date_of_birth=date_of_birth, ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, date_of_birth, password): """ Creates and saves a superuser with the given email, date of birth and password. """ user = self.create_user( email, password=password, date_of_birth=date_of_birth, ) user.is_admin = True user.save(using=self._db) return user class MyUser(AbstractBaseUser): email = models.EmailField( verbose_name="email address", max_length=255, unique=True, ) date_of_birth = models.DateField() is_active = models.BooleanField(default=True) is_admin = models.BooleanField(default=False) objects = MyUserManager() USERNAME_FIELD = "email" REQUIRED_FIELDS = ["date_of_birth"] def get_full_name(self): # The user is identified by their email address return self.email def get_short_name(self): # The user is identified by their email address return self.email def __str__(self): # __unicode__ on Python 2 return self.email def has_perm(self, perm, obj=None): "Does the user have a specific permission?" # Simplest possible answer: Yes, always return True def has_module_perms(self, app_label): "Does the user have permissions to view the app `app_label`?" # Simplest possible answer: Yes, always return True @property def is_staff(self): "Is the user a member of staff?" # Simplest possible answer: All admins are staff return self.is_admin
然后是admin.py
class UserAdmin(BaseUserAdmin): # The forms to add and change user instances form = UserChangeForm add_form = UserCreationForm # The fields to be used in displaying the User model. # These override the definitions on the base UserAdmin # that reference specific fields on auth.User. list_display = ("email", "date_of_birth", "is_admin") list_filter = ("is_admin",) fieldsets = ( (None, {"fields": ("email", "password")}), ("Personal info", {"fields": ("date_of_birth",)}), ("Permissions", {"fields": ("is_admin",)}), ) # add_fieldsets is not a standard ModelAdmin attribute. UserAdmin # overrides get_fieldsets to use this attribute when creating a user. add_fieldsets = ( (None, { "classes": ("wide",), "fields": ("email", "date_of_birth", "password1", "password2")} ), ) search_fields = ("email",) ordering = ("email",) filter_horizontal = () # Now register the new UserAdmin... admin.site.register(MyUser, UserAdmin) # ... and, since we"re not using Django"s built-in permissions, # unregister the Group model from admin. admin.site.unregister(Group)
最后是settings.py
AUTH_USER_MODEL = "customauth.MyUser" AUTHENTICATION_BACKENDS = ( "accounts.backends.LoginBackend", )
這樣一個自定義的用戶模型就弄好了,接下來是自定義登錄字段
class LoginBackend(object): def authenticate(self, username=None, password=None): if username: #email if re.match("^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$", username) != None: try: user = User.objects.get(email=username) if user.check_password(password): return user except User.DoesNotExist: return None #mobile elif len(username)==11 and re.match("^(1[3458]d{9})$", username) != None: try: user = User.objects.get(mobile=username) if user.check_password(password): return user except User.DoesNotExist: return None #nick else: try: user = User.objects.get(username=username) if user.check_password(password): return user except User.DoesNotExist: return None else: return None def get_user(self, user_id): try: return User.objects.get(pk=user_id) except User.DoesNotExist: return None
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/38077.html
摘要:自定義一個用戶很簡單然后是最后是這樣一個自定義的用戶模型就弄好了,接下來是自定義登錄字段 自定義一個用戶很簡單models.py from django.db import models from django.contrib.auth.models import ( BaseUserManager, AbstractBaseUser ) class MyUserManage...
摘要:自定義一個用戶很簡單然后是最后是這樣一個自定義的用戶模型就弄好了,接下來是自定義登錄字段 自定義一個用戶很簡單models.py from django.db import models from django.contrib.auth.models import ( BaseUserManager, AbstractBaseUser ) class MyUserManage...
摘要:另外一個字段用于儲存突出顯示的代碼的表示形式。這將確保認證用戶擁有讀寫權限,而未認證用戶只有讀的權限。唯一的限制是必須是。 官網地址目前,我們的API沒有對如 edit 或者 delect做出任何限制。我們希望有一些更加高級的功能能夠做到: Code snippets 應該永遠和創建者相關 只有認證的用戶才能夠創建snippets 只有創建者才能更新或者刪除他的snippet 沒有認...
摘要:另外一個字段用于儲存突出顯示的代碼的表示形式。這將確保認證用戶擁有讀寫權限,而未認證用戶只有讀的權限。唯一的限制是必須是。 官網地址目前,我們的API沒有對如 edit 或者 delect做出任何限制。我們希望有一些更加高級的功能能夠做到: Code snippets 應該永遠和創建者相關 只有認證的用戶才能夠創建snippets 只有創建者才能更新或者刪除他的snippet 沒有認...
摘要:另外一個字段用于儲存突出顯示的代碼的表示形式。這將確保認證用戶擁有讀寫權限,而未認證用戶只有讀的權限。唯一的限制是必須是。 官網地址目前,我們的API沒有對如 edit 或者 delect做出任何限制。我們希望有一些更加高級的功能能夠做到: Code snippets 應該永遠和創建者相關 只有認證的用戶才能夠創建snippets 只有創建者才能更新或者刪除他的snippet 沒有認...
閱讀 1411·2021-10-08 10:04
閱讀 733·2021-09-07 09:58
閱讀 2912·2019-08-30 15:55
閱讀 2424·2019-08-29 17:21
閱讀 2126·2019-08-28 18:04
閱讀 3075·2019-08-28 17:57
閱讀 715·2019-08-26 11:46
閱讀 2228·2019-08-23 17:20