这是字段集中的重复字段

时间:2018-11-07 01:23:13

标签: django python-3.x django-admin

此类引发系统检查Django错误。它引发的特定错误如下。我浏览了以前的帖子,发现这是由于迁移到django 2.1并在域tuple修复该问题的末尾添加逗号引起的错误。这对我不起作用。任何帮助将不胜感激。

SystemCheckError: System check identified some issues:
ERRORS:
<class 'app.admin.user.UserAdmin'>: (admin.E012) There are duplicate field(s) in 'fieldsets[1][1]'.
<class 'app.admin.user.UserAdmin'>: (admin.E012) There are duplicate field(s) in 'fieldsets[2][1]'.
<class 'app.admin.user.UserAdmin'>: (admin.E012) There are duplicate field(s) in 'fieldsets[3][1]'.

@register(User)
class UserAdmin(ModelAdmin):
    fieldsets = (
        (None, {'fields': ('email', 'password',)}),
        ('Personal info', {
            'fields': ('first_name', 'last_name', 'email',)}),
        ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions',)}),
        ('Important dates', {'fields': ('last_login', 'date_joined',)})
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2',),
        }),
    )
    list_display = ('email', 'first_name', 'last_name', 'username', 'is_staff')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups',)
    search_fields = ('email', 'first_name', 'last_name',)
    ordering = ('email',)
    filter_horizontal = ('groups', 'user_permissions',)

2 个答案:

答案 0 :(得分:0)

我有完全相同的错误,尽管似乎“电子邮件”在每个元组中都是唯一的,但在“字段集”的所有元组中也必须是唯一的。

  

(None, {'fields': ('email', 'password',)})

您所说的“电子邮件”是一个基本字段,而

  

('Personal info', { 'fields': ('first_name', 'last_name', 'email',)})

表示“电子邮件”是“个人信息”字段。您必须选择。

按照@ Selcuk的建议,删除第二个是解决方案。

答案 1 :(得分:0)

正如@Selcuk 和@andramos 已经建议的那样,您提到了 'email' 两次。看看你的fieldsets

第一个:

(None, {'fields': ('email', 'password',)}),
                     ^^^

第二个:

('Personal info', {'fields': ('first_name', 'last_name', 'email',)}),
                                                           ^^^

就您而言,从 'email' 中删除 'Personal info' 将解决您的问题:

fieldsets = (
    (None, {'fields': ('email', 'password',)}),
    ('Personal info', {
        'fields': ('first_name', 'last_name',)}),
    ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser',
                                'groups', 'user_permissions',)}),
    ('Important dates', {'fields': ('last_login', 'date_joined',)})
)
add_fieldsets = (
    (None, {
        'classes': ('wide',),
        'fields': ('email', 'password1', 'password2',),
    }),
)

official document 中有一个有用的示例来自定义用户模型、用户表单和用户管理。

例如,MyUser 模型被定义为使用 email 字段,优先于 username

class MyUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
    )
    date_of_birth = models.DateField()
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = MyUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['date_of_birth']

admin.py 文件中:

class UserAdmin(BaseUserAdmin):
    # 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 = ()