python django models中自定义用户认证
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| from django.contrib.auth.models import ( |
| BaseUserManager, AbstractBaseUser |
| ) |
| |
| class UserProfileManager(BaseUserManager): |
| def create_user(self, email, name, 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), |
| name=name, |
| ) |
| |
| user.set_password(password) |
| user.save(using=self._db) |
| return user |
| |
| def create_superuser(self, email, name, password=None): |
| """ |
| Creates and saves a superuser with the given email, date of |
| birth and password. |
| """ |
| user = self.create_user( |
| email, |
| password=password, |
| name=name, |
| ) |
| user.is_admin = True |
| user.save(using=self._db) |
| return user |
| |
| class UserProfile(AbstractBaseUser): |
| email = models.EmailField( |
| verbose_name='email address', |
| max_length=255, |
| unique=True, |
| ) |
| |
| name = models.CharField(max_length=32) |
| is_active = models.BooleanField(default=True) |
| is_admin = models.BooleanField(default=False) |
| bind_hosts = models.ManyToManyField('BindHost',blank=True) |
| host_groups = models.ManyToManyField('HostGroups',blank=True) |
| |
| objects = UserProfileManager() |
| |
| USERNAME_FIELD = 'email' |
| REQUIRED_FIELDS = ['name'] |
| |
| def __str__(self): |
| return 'email:'+self.email |
| |
| def has_perm(self, perm, obj=None): |
| "Does the user have a specific permission?" |
| |
| return True |
| |
| def has_module_perms(self, app_label): |
| "Does the user have permissions to view the app `app_label`?" |
| |
| return True |
| |
| @property |
| def is_staff(self): |
| "Is the user a member of staff?" |
| |
| return self.is_admin |
| 在settings.py中加上 |
| |
| AUTH_USER_MODEL = 'web.UserProfile' |
django amdin自定义
| |
| |
| |
| from django import forms |
| from django.contrib import admin |
| from django.contrib.auth.models import Group |
| from django.contrib.auth.admin import UserAdmin as BaseUserAdmin |
| from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| |
| from web.models import UserProfile |
| class UserCreationForm(forms.ModelForm): |
| """A form for creating new users. Includes all the required |
| fields, plus a repeated password.""" |
| password1 = forms.CharField(label='Password', widget=forms.PasswordInput) |
| password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) |
| |
| class Meta: |
| model = UserProfile |
| fields = ('email', 'name','is_admin','bind_hosts','host_groups') |
| |
| def clean_password2(self): |
| |
| password1 = self.cleaned_data.get("password1") |
| password2 = self.cleaned_data.get("password2") |
| if password1 and password2 and password1 != password2: |
| raise forms.ValidationError("Passwords don't match") |
| return password2 |
| |
| def save(self, commit=True): |
| |
| user = super().save(commit=False) |
| user.set_password(self.cleaned_data["password1"]) |
| if commit: |
| user.save() |
| return user |
| |
| class UserChangeForm(forms.ModelForm): |
| """A form for updating users. Includes all the fields on |
| the user, but replaces the password field with admin's |
| password hash display field. |
| """ |
| password = ReadOnlyPasswordHashField() |
| |
| class Meta: |
| model = UserProfile |
| fields = ('email', 'password', 'name', 'is_active', 'is_admin') |
| |
| def clean_password(self): |
| |
| |
| |
| return self.initial["password"] |
| |
| class UserAdmin(BaseUserAdmin): |
| |
| form = UserChangeForm |
| add_form = UserCreationForm |
| |
| |
| |
| |
| list_display = ('email', 'name', 'is_admin') |
| list_filter = ('is_admin',) |
| fieldsets = ( |
| (None, {'fields': ('email', 'password')}), |
| ('Personal info', {'fields': ('name',)}), |
| ('Permissions', {'fields': ('is_admin','bind_hosts','host_groups')}), |
| ) |
| |
| |
| |
| add_fieldsets = ( |
| (None, { |
| 'classes': ('wide',), |
| 'fields': ('email', 'name', 'password1', 'password2','is_admin','bind_hosts','host_groups'), |
| }), |
| ) |
| search_fields = ('email',) |
| ordering = ('email',) |
| filter_horizontal = ('bind_hosts','host_groups') |
| |
| |
| admin.site.register(UserProfile, UserAdmin) |
| |
| |
| admin.site.unregister(Group) |
| |
| |
| 在admin.py中导入py文件,就会自动执行注册(admin.site.register(UserProfile, UserAdmin)) |
| from web import customer_user_admin |
| |
django admin用法


在trigger 下面显示expecetion

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)