user registration ERROR

hello everyone here i have a problem with django errors
i have wrote this code for admin registration but there is an error i don’t undrestand
look at my codes i have wrote in admin.py file down below and the error i got:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as BaseUserAdmin
from .forms import UserCreationForm , UserChangeForm
from django.contrib.auth.models import Group
from .models import User


class UserAdmin(BaseUserAdmin):
    form = UserChangeForm
    add_form = UserCreationForm

    list_display = ('email' , 'phone_number' , 'is_admin')
    list_filter = ["is_admin"]
    fieldsets = (
        (None , {'fields':('email' , 'phone_number' , 'full_name' , 'password')}),
        ('permissions' , {'fields':('is_active' , 'is_admin' , 'last_login')})
    )
    add_fieldsets=(
        (None , {'fields':('phone_number' , 'email', 'full_name', 'password1' , 'password2')}),
    )

    search_fields = ('email' , 'full_name')
    ordering = ('full_name' , )
    filter_horizontal = ()

admin.site.unregister(Group)
admin.site.register(User , UserAdmin)

and the error is :
<class ‘accounts.admin.UserAdmin’>: (admin.E116) The value of ‘list_filter[0]’ refers to ‘user__is_admin’, which does not refer to a Field.

i have set “is_admin” for the value of list_filter

down below is codes i wrote in model.py

from django.db import models
from django.contrib.auth.models import AbstractBaseUser
from .managers import UserManager


class User(AbstractBaseUser):
    email = models.EmailField(max_length=255 , unique=True)
    full_name = models.CharField(max_length=100)
    phone_number = models.CharField(max_length=11 , unique=True)
    is_active = True
    is_admin = False

    objects = UserManager()

    USERNAME_FIELD = 'phone_number'
    REQUIRED_FIELDS = ['email' , 'full_name']

    def __str__(self) -> str:
        return self.email
    
    def has_perm(self , perm , obj=None):
        return True
    
    def has_module_perm(self , app_label):
        return True
    
    def is_staff(self):
        return self.is_admin

what’s the problem you think?

The problem is that you have not defined is_active and is_admin as database fields, you have defined them as class-level attributes of the base class. Since they are not fields in the model, they are not accessible by the admin as if they were. You probably want to define them as models.BooleanField with the appropriate default. (See how they’re created in AbstractUser.)

1 Like

list_filter is expecting the names of fields. For example, like models.EmailField(), models.CharField().

But in your User model you have:

    is_active = True
    is_admin = False

Neither of these are fields, they’re variables.

You won’t be able to change them for each individual user, and they won’t be saved to the database, and you can’t filter by them in Django Admin.

Looking at the example in the documentation for how to create a custom User model, they should be:

    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
1 Like

that’s it
it worked
thank you

yesss ur right
thakns