Unknown field(s) (username) specified for User. Check fields/fieldsets/exclude attributes of class CustomUserAdmin

10.4k Views Asked by At

After creating a custom user for my python django app, i started getting the error stated on the title. It only happens when i wan't to add a new user from the admin panel that's when i get the error "Unknown field(s) (username) specified for User. Check fields/fieldsets/exclude attributes of class CustomUserAdmin." I have tried searching for answers everywhere on the internet but no luck. admin.py:

# accounts/admin.py
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import User

class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = User
    list_display = ['email', 'first_name','last_name', 'image',  'country_code','country', 'phone','state_province','city_town','address', 'postal_code',]
    add_fieldsets = UserAdmin.add_fieldsets + (
        (None, {'fields': ('email', 'first_name', 'last_name',  'image', 'country_code','country', 'phone','state_province','city_town','address', 'postal_code',)}),
    )
    fieldsets = (
        (None, {
            "fields": (
                ('email', 'first_name', 'last_name', 'image', 'is_staff',  'country_code','country', 'phone','state_province','city_town','address', 'postal_code',)
                
            ),
        }),
    )
    search_fields = ('email', 'first_name', 'last_name')
    ordering = ('email',)
    
admin.site.register(User, CustomUserAdmin)

models.py:

    from django.contrib.auth.models import AbstractUser, BaseUserManager ## A new class is imported. ##
from django.db import models
from django_countries.fields import CountryField
from django.utils.translation import ugettext_lazy as _



from django.contrib.auth.models import AbstractUser
from django.db import models


class UserManager(BaseUserManager):
    """Define a model manager for User model with no username field."""

    use_in_migrations = True

    def _create_user(self, email, password, **extra_fields):
        """Create and save a User with the given email and password."""
        if not email:
            raise ValueError('The given email must be set')
        email = self.normalize_email(email)
        user = self.model(email=email, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, email, password=None, **extra_fields):
        """Create and save a regular User with the given email and password."""
        extra_fields.setdefault('is_staff', False)
        extra_fields.setdefault('is_superuser', False)
        return self._create_user(email, password, **extra_fields)

    def create_superuser(self, email, password, **extra_fields):
        """Create and save a SuperUser with the given email and password."""
        extra_fields.setdefault('is_staff', True)
        extra_fields.setdefault('is_superuser', True)

        if extra_fields.get('is_staff') is not True:
            raise ValueError('Superuser must have is_staff=True.')
        if extra_fields.get('is_superuser') is not True:
            raise ValueError('Superuser must have is_superuser=True.')

        return self._create_user(email, password, **extra_fields)




class User(AbstractUser):
    username = None
    email = models.EmailField(_('email address'), unique=True)
    country_code = models.CharField(max_length=250)
    phone = models.IntegerField(unique=True, null=True)
    country = CountryField(max_length=250)
    state_province = models.CharField(max_length=250)
    city_town = models.CharField(max_length=250)
    address = models.CharField(max_length=250)
    postal_code = models.CharField(max_length=250)
    image = models.ImageField(default='default.png')


    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []
    objects = UserManager() ## This is the new line in the User model. ##
    # add additional fields in here

    def __str__(self):
        return self.email
3

There are 3 best solutions below

5
On

Remove UserAdmin.add_fieldsets +

This means expand original fieldsets that includes original username. Since your custom user model does not have username, it gives error.

0
On

It works for me after adding the following code in my app/admin.py CustomeUserAdmin Class:

add_fieldsets = (
    (None, {
        'classes': ('wide',),
        'fields': ('email', 'first_name', 'last_name', 'password1', 'password2'),
    }),
)

Replace in the fields regarding your customization.

For more, read the docs.

0
On

Had the same problem and found out I was missing a comma at the end of the new field, as it only accept a tupple:

('Additional info', {
        'fields': ('New Field')
    })

Changed to:

('Additional info', {
        'fields': ('New Field',)
    })

Notice the added comma.