[英]Django admin How to solve the problem that all parts are not visible?
I created a system with Django. I create an abstract user model with some custom fields.我用 Django 创建了一个系统。我用一些自定义字段创建了一个抽象用户 model。 My form is working but in the admin page I cannot see my custom models.
我的表单正在运行,但在管理页面中我看不到我的自定义模型。
models.py模型.py
class UserProfile(AbstractUser):
ranks = (
...
('cfo', 'Chief Financial Officier'),
)
comp_name = models.CharField(max_length=200, default="Choose")
user_id = models.UUIDField(default=uuid.uuid4(), editable=False, unique=True)
username = models.CharField(max_length=500, unique=True)
first_name = models.CharField(max_length=200)
last_name = models.CharField(max_length=200)
password = models.CharField(max_length=50)
email = models.EmailField(max_length=254)
rank = models.CharField(max_length=200, choices=ranks)
def __str__(self):
return self.username
class CompanyProfile(models.Model):
...
admin.py管理员.py
admin.site.register(CompanyProfile)
class CustomUser(UserAdmin):
add_form = SignUpForm
model = UserProfile
form = SignUpChangeForm
list_display = ('username', 'first_name', 'email', 'last_name', 'rank', 'comp_name')
fieldsets = (
(None, {'fields': ('email', 'password')}),
)
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'password1', 'password2', 'rank', 'comp_name')}
),
)
search_fields = 'email'
admin.site.register(UserProfile, UserAdmin)
As you can see in admin view there is no ranks
and company name
field.正如您在管理视图中看到的那样,没有
ranks
和company name
字段。 How can I solve it?我该如何解决?
You should add them to fieldsets aswell, from the docs您也应该从文档中将它们添加到字段集中
Note : If you are using a custom ModelAdmin which is a subclass of django.contrib.auth.admin.UserAdmin, then you need to add your custom fields to fieldsets (for fields to be used in editing users) and to add_fieldsets (for fields to be used when creating a user).
注意:如果您使用的是自定义 ModelAdmin,它是 django.contrib.auth.admin.UserAdmin 的子类,那么您需要将自定义字段添加到 fieldsets(用于编辑用户的字段)和 add_fieldsets(用于字段创建用户时使用)。 For example:
例如:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.