1

I have a custom user model without username, but using email instead.

class User(AbstractUser):

    username = None
    email = models.EmailField(_("email address"), unique=True)
    uuid = models.CharField(max_length=36, default=uuid4, unique=True)
    USERNAME_FIELD = "email"
    REQUIRED_FIELDS = []
    objects = CustomUserManager()

    def __str__(self):
        return self.email

And a project model that has multiple users:

from user.models import User

class Project(models.Model):

    project_id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=256, unique=True, blank=False)
    created = models.DateTimeField(auto_now_add=True)
    description = models.TextField(max_length=10000, default="")
    slug = models.SlugField(max_length=256, unique=True)
    created_by = CurrentUserField(related_name='creator')
    users = models.ManyToManyField(User, related_name='users')
    ....

My UserAdmin model looks like this:

class CustomUserAdmin(UserAdmin):
    list_display = ("email", "is_staff", "is_superuser")
    readonly_fields = ("last_login", "date_joined", "uuid")
    ordering = ("email",)
    fieldsets = (
        (
            "Fields",
            {
                "fields": (
                    "email",
                    "uuid",
                    "date_joined",
                    "last_login",
                    "is_active",
                    "is_staff",
                    "is_superuser",
                    "groups",
                    "user_permissions",
                    "password",
                )
            },
        ),
    )


admin.site.register(User, CustomUserAdmin)

However, when I go to admin view to a project instance and try to add user I get:

FieldError at /admin/user/user/add/ Unknown field(s) (username) specified for User. Check fields/fieldsets/exclude attributes of class CustomUserAdmin. Request Method: GET Request URL: http://localhost:8000/admin/user/user/add/?_to_field=id&_popup=1 Django Version: 3.2.11 Exception Type: FieldError Exception Value:
Unknown field(s) (username) specified for User. Check fields/fieldsets/exclude attributes of class CustomUserAdmin.

I added my user model as a default model in settings, so I don't know where the username field here comes from.

Soerendip
  • 7,684
  • 15
  • 61
  • 128

1 Answers1

1

The username field is still part of the search_fields and the add_fieldsets. You thus should remove the username field there:

class CustomUserAdmin(UserAdmin):
    list_display = ('email', 'is_staff', 'is_superuser')
    readonly_fields = ('last_login', 'date_joined', 'uuid')
    ordering = ('email',)
    search_fields = ('first_name', 'last_name', 'email')  # 🖘 no username
    fieldsets = (
        (
            'Fields',
            {
                'fields': (
                    'email',
                    'uuid',
                    'date_joined',
                    'last_login',
                    'is_active',
                    'is_staff',
                    'is_superuser',
                    'groups',
                    'user_permissions',
                    'password',
                )
            },
        ),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('email', 'password1', 'password2'),
            #              🖞 without username
        }),
    )


admin.site.register(User, CustomUserAdmin)
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555