5

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
Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42

3 Answers3

6

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.

ha-neul
  • 3,058
  • 9
  • 24
  • 2
    Hi, I am experiencing the same problem and I don't have add_fieldsets in my code. I inherited AbstractBaseUser and PermissionsMixin in my User class. I don't know what could be causing the issue. – Bruce May 27 '21 at 23:05
  • @Bruce, did you find any answer? I'm having the problem you described. – André Carvalho Aug 15 '21 at 14:02
  • 1
    @AndréCarvalho I changed the code and used what is in the docs. Here https://docs.djangoproject.com/en/3.2/topics/auth/customizing/#a-full-example – Bruce Aug 15 '21 at 22:53
  • I have the same problem. – Soerendip Jan 18 '22 at 20:11
  • @AndréCarvalho @Bruce @Soren If your custom model doesn't have `username`, you have to override `fieldsets` in the manager - because `UserAdmin` defaults to those values in its own `fieldsets` attribute. If you don't override `fieldsets`, the default gets used and that's why you get this error. – Vegard Feb 25 '23 at 17:54
2

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.

Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
0

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.

ouflak
  • 2,458
  • 10
  • 44
  • 49