django 以下是字段集中重复字段

bwntbbo3  于 2023-08-08  发布在  Go
关注(0)|答案(4)|浏览(119)

这个类引发了一个django系统检查错误。它引发的具体错误如下所示。我看过以前的帖子,建议这是一个错误,导致迁移到django 2.1,并在字段元组的末尾添加一个逗号修复了它。这对我没用。如有任何帮助,我们将不胜感激。

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',)

字符串

ar5n3qh5

ar5n3qh51#

我有完全相同的错误,虽然似乎'email'在每个元组中是唯一的,但它必须在所有'fieldset'的元组中是唯一的。
第一个月
你说“电子邮件”是一个基本的领域
('Personal info', { 'fields': ('first_name', 'last_name', 'email',)})
是说“电子邮件”是一个“个人信息”字段。你必须做出选择
删除第二个是解决方案,正如@Selcuk所建议的那样。

vom3gejh

vom3gejh2#

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

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

字符串
和第二:

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


在您的情况下,从'Personal info'中删除'email'将解决您的问题:

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',),
    }),
)


在官方文档中有一个有用的例子来定制用户模型,用户表单和用户admin。
例如,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 = ()

z5btuh9x

z5btuh9x3#

同一字段“email”不能在“fieldsets”**中的所有元组中使用两次,如下所示,因此需要删除其中一个元组:

fieldsets = (              # Here
        (None, {'fields': ('email', 'password',)}),
        ('Personal info', {                       # Here
            'fields': ('first_name', 'last_name', 'email',)}),
        ('Permissions', {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions',)}),
        ('Important dates', {'fields': ('last_login', 'date_joined',)})
    )

字符串

eulz3vhy

eulz3vhy4#

我遇到了同样的错误,可以这样解决:

from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .models import User # My custom user model

@admin.register(User)
class UserAdmin(BaseUserAdmin):
    readonly_fields = ["date_joined"]

    fieldsets = (
        (None, {"fields": ("username", "password")}),
        (("Personal info"), {"fields": ("first_name", 
                                        "last_name", 
                                        "email", # here I added my New 
                                        "image", # fields in custom User
                                        "gender", 
                                        "language"
                                        )}),
        (
            ("Permissions"),
            {
                "fields": (
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "groups",
                    "user_permissions",
                ),
            },
        ),
        (("Important dates"), {"fields": ("last_login", "date_joined")}),
    )

字符串

相关问题