Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Custom User Model errors ((admin.E108) The value of 'list_display[2]' refers to 'first_name', which is not a callable

Tags:

django

I have a code to Extend User Model by Using a Custom Model Extending AbstractBaseUser. I got this error message

(ERRORS: : (admin.E108) The value of 'list_display[2]' refers to 'first_name', which is not a callable, an attribute of 'UserAdmin', or an attribute or method on 'towns.MyUser'. : (admin.E108) The value of 'list_display[3]' refers to 'last_name', which is not a callable, an attribute of 'UserAdmin', or an attribute or method on 'towns.MyUser'.

The models file :

from django.contrib.auth.models import (
        BaseUserManager, AbstractBaseUser
    )


class MyUserManager(BaseUserManager):
    def create_user(self, username, email, password=None):
        if not email:
            raise ValueError('email address is required')

        user = self.model(
                username = username,
                email = self.normalize_email(email)
            )
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, email, password=None):
        user = self.create_user(
                username, email, password=password
            )

        user.is_admin = True
        user.is_staff = True

        user.save(using=self._db)
        return user


class MyUser (AbstractBaseUser):
    username = models.CharField(
            max_length=255,
            unique=True
        )

    email = models.EmailField(
            max_length=255,
            unique=True,
            verbose_name='email address'

        )

    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(default=False)

    objects = MyUserManager()
    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

The form file:

from django.contrib.auth import get_user_model
from django.db.models import Q

User = get_user_model() 

from django import forms 

class UserCreationForm(forms.ModelForm):
    password1 = forms.CharField(label='password', widget=forms.PasswordInput)
    password2 = forms.CharField(label='password confirmation', widget=forms.PasswordInput)

    class Meta:
        model = User
        fields = ['username', 'email']

    def clean_password(self):
        password1 = self.cleaned_data.get('password1')
        password2 = self.cleaned_data.get('password2')
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("passwords do not match")
        return password2

    def save(self, commit=True):
        user = super(UserCreationForm, self).save(commit=False)
        user.set_password(self.cleaned_data['password1'])

        if commit:
            user.save()
        return user



class UserLoginForm(forms.Form):
    query = forms.CharField(label='Username or Email')
    password = forms.CharField(label='Password',widget=forms.PasswordInput)

    def clean(self, *args, **kwargs):
        query = self.cleaned_data.get('query')
        password = self.cleaned_data.get('password')

        user_qs_final = User.objects.filter(
                Q(username__iexact=query) |
                Q(email__iexact=query).distinct() 

            )
        if not user_qs_final.exists() and user_qs_final.count != 1:
            raise forms.ValidationError('Invalid credentials = user does not exist')

        user_obj = user_qs_final.first()
        if not user.obj.check_password(password):
            raise forms.ValidationError(' password is not correct')

        self.cleaned_data["user_obj"] = user_obj
        return super(UserLoginForm, self).clean(*args, **kwargs)

The admin file:

from django.contrib import admin
from django.contrib.auth.models import Group
from django.contrib.auth.admin import UserAdmin  as BaseUserAdmin

from .forms import UserCreationForm
from .models import MyUser


class UserAdmin(BaseUserAdmin):
    add_form =  UserCreationForm

    List_display = ('username', 'email', 'is_admin')
    list_filter = ('is_admin',)

    fieldsets = (
        (None, {'fields': ('username', 'email','password')}),

        ('Permissions', {'fields': ('is_admin',)}),
    )

    search_fields =  ('username', 'email')
    ordering = ('username','email')

    filter_horizontal = ()

admin.site.register(MyUser,UserAdmin)




admin.site.unregister(Group)

also I have changed the AUTH_USER_MODEL in the settings file to be

AUTH_USER_MODEL = 'towns.MyUser'

any clue how get to resolve this error

Note : I do not really needs those fields (first_name), (last_name)

like image 885
Saleh Avatar asked May 26 '18 06:05

Saleh


1 Answers

There is a typo in your admin class as you are not overriding the default list_display option which contains first_name. It should be list_display instead of List_display inside UserAdmin:

class UserAdmin(BaseUserAdmin):
    add_form =  UserCreationForm

    list_display = ('username', 'email', 'is_admin')
    list_filter = ('is_admin',)

    fieldsets = (
        (None, {'fields': ('username', 'email','password')}),

        ('Permissions', {'fields': ('is_admin',)}),
    )

    search_fields =  ('username', 'email')
    ordering = ('username','email')

    filter_horizontal = ()

admin.site.register(MyUser,UserAdmin)
like image 51
neverwalkaloner Avatar answered Oct 16 '22 01:10

neverwalkaloner