在Django中,我使用(AbstractUser)替换了用户模型。然后在管理面板区域,字段是无序的。生成新用户时,密码未加密,用户保存时密码未加密。但这样我就无法进入管理面板了。用户名或密码不匹配返回错误。
在model.py
# from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser
from django.db import models
# UserModel = get_user_model()
class UserAgent_mod(AbstractUser):
phone_number = models.CharField(
max_length=9,
blank=True,
null=True,
)
profile_image = models.ImageField(
upload_to='photos_user_agent',
)
manage_properties = models.ManyToManyField(
to='property_app.Property_mod',
related_name='manage_properties',
blank=True
)
def __str__(self):
return f'{self.first_name} {self.last_name}'
class Meta:
verbose_name='User Agent'
verbose_name_plural = 'Users Agents'
在admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
UserModel = get_user_model()
@admin.register(UserModel)
class UserAgentAdmin(admin.ModelAdmin):
list_display = (
'username',
'first_name',
'last_name',
'phone_number',
'email',
)
list_filter = (
'first_name',
'last_name',
)
在settings.py
AUTH_USER_MODEL= 'user_agent_app.UserAgent_mod'
问题的演示结果
在admin.py文件中放入以下代码:
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAgent_mod as BaseUserAdmin
class UserAgentAdmin(BaseUserAdmin):
fieldsets = BaseUserAdmin.fieldsets
ADDITIONAL_USER_FIELDS = (
(None, {'fields': ('phone_number','profile_image','manage_properties')}),
)
fieldsets = fieldsets + ADDITIONAL_USER_FIELDS
list_display = (
'username',
'first_name',
'last_name',
'phone_number',
'email',
)
list_filter = (
'first_name',
'last_name',
)
admin.site.register(get_user_model, UserAgentAdmin)
当你使用AbstarctUser
时,你需要通过django管理面板添加用户,你也应该使用UserCreationForm
和UserChangeForm
,这样密码将被加密。
为此,在您的用户应用程序中创建一个forms.py
文件,并向其中添加以下代码:
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import UserAgent_mod
class UserCreationForm(UserCreationForm):
# If you want to give superuser privileges to the staff users, override the save method
def save(self, commit=True):
user = self.instance
if user.is_staff:
user.is_superuser = True
return super().save(commit=True)
class Meta:
model = UserAgent_mod
fields = ('email', 'first_name', 'last_name') # add any other field you want
class UserChangeForm(UserChangeForm):
class Meta:
model = UserAgent_mod
fields = ('email', 'first_name', 'last_name') # add any other field you want
也添加下面的代码到你的admin.py
:
(根据需要更改字段)
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .forms import UserCreationForm, UserChangeForm
from .models import UserAgent_mod
class UserAdmin(UserAdmin):
add_form = UserCreationForm
form = UserChangeForm
model = UserAgent_mod
list_display = ('email', 'is_staff', 'is_active',)
list_filter = ('email', 'is_staff', 'is_active',)
fieldsets = (
(None, {'fields': ('email', 'password', 'first_name', 'last_name')}),
('Permissions', {'fields': ('is_staff', 'is_active')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'password1', 'password2', 'first_name', 'last_name', 'is_staff', 'is_active')}
),
)
search_fields = ('email',)
ordering = ('email',)
admin.site.register(UserAgent_mod, UserAdmin)
您可以在本文中阅读更多关于AbstractUser
的信息。