在 Django 中创建超级用户时如何定义额外字段?
How to define extra fields when creating super user in Django?
我的models.py
里有这个:
class UserManager(BaseUserManager):
def create_user(self, name, password=None):
"""
Creates and saves a User with the given name and password.
"""
if not name:
raise ValueError('A user must have a name.')
user = self.model()
user.set_password(password)
user.save(using=self._db)
return user
def create_staffuser(self, name, password):
"""
Creates and saves a staff user with the given name and password.
"""
user = self.create_user(
name,
password=password,
)
user.staff = True
user.save(using=self._db)
return user
def create_superuser(self, name, password, id):
"""
Creates and saves a superuser with the given name and password.
"""
user = self.create_user(
name,
id,
password=password,
)
user.staff = True
user.admin = True
user.save(using=self._db)
return user
我在我的 create_superuser
方法中添加字段 id
的原因是它是我模型中的必填字段,我认为当 运行 时我也需要包含它python manage.py createsuperuser
。我的问题是我不知道如何自定义超级用户创建。有了这个,我总是得到错误 TypeError: create_superuser() missing 1 required positional argument: 'id'
。我还尝试从函数中删除 id。然后该过程顺利进行,但我无法登录管理站点,它说用户名或密码不正确。
我的 admin.py
:
里有这个
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .forms import UserAdminCreationForm, UserAdminChangeForm
User = get_user_model()
# Remove Group Model from admin. We're not using it.
admin.site.unregister(Group)
class UserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserAdminChangeForm
add_form = UserAdminCreationForm
# 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 = ['name', 'admin']
list_filter = ['admin']
fieldsets = (
(None, {'fields': ('name', 'password')}),
('Personal info', {'fields': ()}),
('Permissions', {'fields': ('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': ('name', 'password1', 'password2')}
),
)
search_fields = ['name']
ordering = ['name']
filter_horizontal = ()
admin.site.register(User, UserAdmin)
我是否需要修改 admin.py
文件才能使其正常工作,或者是否有其他一些明显的原因导致它不起作用?
无论如何,手动给 id 值确实是不好的做法。 ID应该是自动生成的。但是如果你真的想获取id作为函数参数,那么你需要重写Django创建超级用户的命令。
您可以在此处阅读自定义 Django 管理命令的详细信息。
https://docs.djangoproject.com/en/3.2/howto/custom-management-commands/
我的models.py
里有这个:
class UserManager(BaseUserManager):
def create_user(self, name, password=None):
"""
Creates and saves a User with the given name and password.
"""
if not name:
raise ValueError('A user must have a name.')
user = self.model()
user.set_password(password)
user.save(using=self._db)
return user
def create_staffuser(self, name, password):
"""
Creates and saves a staff user with the given name and password.
"""
user = self.create_user(
name,
password=password,
)
user.staff = True
user.save(using=self._db)
return user
def create_superuser(self, name, password, id):
"""
Creates and saves a superuser with the given name and password.
"""
user = self.create_user(
name,
id,
password=password,
)
user.staff = True
user.admin = True
user.save(using=self._db)
return user
我在我的 create_superuser
方法中添加字段 id
的原因是它是我模型中的必填字段,我认为当 运行 时我也需要包含它python manage.py createsuperuser
。我的问题是我不知道如何自定义超级用户创建。有了这个,我总是得到错误 TypeError: create_superuser() missing 1 required positional argument: 'id'
。我还尝试从函数中删除 id。然后该过程顺利进行,但我无法登录管理站点,它说用户名或密码不正确。
我的 admin.py
:
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .forms import UserAdminCreationForm, UserAdminChangeForm
User = get_user_model()
# Remove Group Model from admin. We're not using it.
admin.site.unregister(Group)
class UserAdmin(BaseUserAdmin):
# The forms to add and change user instances
form = UserAdminChangeForm
add_form = UserAdminCreationForm
# 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 = ['name', 'admin']
list_filter = ['admin']
fieldsets = (
(None, {'fields': ('name', 'password')}),
('Personal info', {'fields': ()}),
('Permissions', {'fields': ('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': ('name', 'password1', 'password2')}
),
)
search_fields = ['name']
ordering = ['name']
filter_horizontal = ()
admin.site.register(User, UserAdmin)
我是否需要修改 admin.py
文件才能使其正常工作,或者是否有其他一些明显的原因导致它不起作用?
无论如何,手动给 id 值确实是不好的做法。 ID应该是自动生成的。但是如果你真的想获取id作为函数参数,那么你需要重写Django创建超级用户的命令。
您可以在此处阅读自定义 Django 管理命令的详细信息。 https://docs.djangoproject.com/en/3.2/howto/custom-management-commands/