Python Django抽象用户
我可以使用Django用户模型为抽象基本用户模型子类化的模型提供的相同身份验证系统吗?如果不能,我们还能做什么?另外,如何配置ModelAdmin以提供对这些模型的管理员访问?您更喜欢使用Django的内置身份验证系统,这可能表明不需要基于抽象基本用户模型替换自定义用户类。您可能需要考虑用一对一关系()来扩展用户类。这将允许您保留Django的所有默认行为,以及添加您希望用户拥有的任何其他字段、方法等。我们发现这对于我们的大多数情况来说是足够的,并且比创建一个全新的使用类要简单得多 无论如何,也就是说,您可以通过继承BaseUserManager并创建一个新的管理器,然后将自定义用户的Python Django抽象用户,python,django,django-models,django-admin,Python,Django,Django Models,Django Admin,我可以使用Django用户模型为抽象基本用户模型子类化的模型提供的相同身份验证系统吗?如果不能,我们还能做什么?另外,如何配置ModelAdmin以提供对这些模型的管理员访问?您更喜欢使用Django的内置身份验证系统,这可能表明不需要基于抽象基本用户模型替换自定义用户类。您可能需要考虑用一对一关系()来扩展用户类。这将允许您保留Django的所有默认行为,以及添加您希望用户拥有的任何其他字段、方法等。我们发现这对于我们的大多数情况来说是足够的,并且比创建一个全新的使用类要简单得多 无论如何,也
对象
字段分配给这个新的管理器来创建自定义身份验证。或者,您可以将自定义用户的对象
字段分配给Django的管理器。只有在需要某种自定义身份验证(例如,使用电子邮件而不是用户名登录)时,才应该创建自己的管理器。这里是一个关于如何做到这一点的文档
要向Django的admin注册自定义用户模型,您需要在特定应用程序的admin.py文件中执行一些操作。首先,创建用于创建和编辑用户的自定义表单,然后在自定义用户管理类中创建UserAdmin子类,并引用以前创建的表单。最后,使用Admin.site.register()
注册新用户Admin。下面是admin.py文件的完整示例:
from django import forms
from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import ReadOnlyPasswordHashField
from customauth.models import MyUser
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 = MyUser
fields = ('email', 'date_of_birth')
def clean_password2(self):
# Check that the two password entries match
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):
# Save the provided password in hashed format
user = super(UserCreationForm, self).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 = MyUser
fields = ['email', 'password', 'date_of_birth', 'is_active', 'is_admin']
def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
class MyUserAdmin(UserAdmin):
# 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, MyUserAdmin)
# ... and, since we're not using Django's built-in permissions,
# unregister the Group model from admin.
admin.site.unregister(Group)
使用admin.py文件后,使用settings.py
文件中的AUTH\u user\u model
变量将自定义模型指定为默认用户模型
AUTH_USER_MODEL = 'customauth.MyUser'
使用自定义用户模型在本文中进行了描述,但我仍然对modeladmin和用于抽象基本用户的admin类感到困惑,该类在文档中没有完全定义。我只对使用MyUserAdmin感到困惑。我还可以使用django用户模型中定义的authenticate函数,或者我必须在Myusermodel中再次定义它,以对任何浏览我的站点的用户进行身份验证@SGARZA62如果您的自定义用户模型包含一个
username
字段,那么Django的内置authenticate()
方法就足够了。如果您的用户模型仅包括(例如)电子邮件作为唯一标识符,那么您必须将电子邮件
指定为用户类中的用户名\u字段
。请通读以下两个交换以了解更多信息:[1][2]它仍然不起作用我是说在以超级用户身份登录admin后我得到了以下信息我是说我成功登录,但它显示>站点管理>您没有编辑任何内容的权限您使用的是什么版本的Django?如果它比1.7旧,那么在声明urlpatterns
之前,在您的urls.py
文件中,从django.contrib import admin导入,然后将admin.autodiscover()
添加到项目的urls.py
文件中。链接到文档:完成后,如果仍然存在权限问题,则可能是您的自定义用户模型需要定义权限。将用户模型()中的PermissionsMixin
子类化,如此答案的代码所示()