here are duplicate field(s) in fieldsets

Viewed 2896

This class raises a systems check django error. The specific error it raises is as pasted below. I have looked through previous posts that suggest it was an error that resulted from migrating to django 2.1 and adding a comma at the end of the fields tuple fixed it. This does not work for me. Any help would be greatly appreciated.

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

I had the exact same error, although it seems that 'email' is unique in each tuple, it has to be unique in all tuples of 'fieldsets'.

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

There you are saying that 'email' is a basic field and

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

is saying that 'email' is a 'Personal info' field. You have to choose.

Removing the second one is the solution, as @Selcuk suggested.

As @Selcuk and @andramos have already suggested, you mentioned 'email' twice. Look at your fieldsets:

The first one:

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

and the second:

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

In your case, removing 'email' from 'Personal info' will solve your issue:

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

There is a useful example in the official document to customize user model, user forms and user admin.

For example, MyUser model is defined to use email field by preferred over 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']

In admin.py file:

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 = ()

You cannot use the same field "email" twice through all tuples in "fieldsets" as shown below so you need to remove one of them:

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