简体   繁体   中英

Model Not showing in Django Admin created using built in AbstractUser

I have created a custom user model using Django built in User and I have added my own fields. When I try to run the code I am getting error. I have searched everywhere and I am not getting the answer to this error.

I have run the code with empty admin.py it worked perfectly for LogIn/Logout and SignUp But nothing my showing in Django admin.

Then after some research I came with the code mentioned below but Still I am getting error!

models.py

from django.db import models
from django.contrib.auth.models import BaseUserManager,AbstractBaseUser

class MyAccountManager(BaseUserManager):
    def create_user(self, email, username, address,phone_number,password=None):
        if not email:
            raise ValueError('Users must have an email address')
        if not username:
            raise ValueError('Users must have a username')

        user = self.model(
            email=self.normalize_email(email),
            username=username,
            address=address,
            phone_number=phone_number,
        )

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

    def create_superuser(self, email, username, address,phone_number,password):
        user = self.create_user(
            email=self.normalize_email(email),
            password=password,
            username=username,
            address=address,
            phone_number=phone_number,
        )
        user.is_admin = True
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user


class CustomUser(AbstractBaseUser):
    email = models.EmailField(verbose_name="email",max_length=60,unique=True)
    username= models.CharField(max_length=30,unique=True)
    phone_number= models.IntegerField()
    address=models.CharField(max_length=300)
    is_active=models.BooleanField(default=True)
    is_admin=models.BooleanField(default=False)
    is_staff=models.BooleanField(default=False)
    is_superuser=models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username','address','phone_number']

    objects = MyAccountManager()

    def __str__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return self.is_admin

    def has_module_perms(self, app_label):
        return True

forms.py

from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm,UserChangeForm
from django import forms
from . models import CustomUser

class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        fields = UserChangeForm.Meta.fields

class UserCreateform(UserCreationForm):

    class Meta(UserCreationForm.Meta):

        model = CustomUser
        fields = UserCreationForm.Meta.fields+('address','phone_number')

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import CustomUser
from .forms import UserCreateform,CustomUserChangeForm

class UserAd(UserAdmin):
    add_form = UserCreateform
    form = CustomUserChangeForm
    model = CustomUser

    list_display = ("email","username","address","phone_number","is_staff")
    list_filter = ("is_staff","is_superuser","is_active","groups")
    search_fields = ("username","email")
    ordering=("username")
    filter_horizontal=("groups","user_permissions",)
    fieldsets = (
     ( None, {"fields": ("username","password")}),
     ( ("Personal Info"),{"fields":("email","address","phone_number")}),
     ( ("Permissions"),{"fields":("is_admin","is_active","is_staff","is_superuser","groups","user_permissions")}),
     ( ("Important Dates"),{"fields":("last_login","date_joined")}),
    )
    add_fieldsets = (
     (
        None,
        {
        "classes":("wide",),
        "fields":("username","password1","password2"),
        },
     ),
    )

admin.site.register(CustomUser, UserAd)

Error Message!

<class 'account.admin.UserAd'>: (admin.E019) The value of 'filter_horizontal[0]' refers to 'groups', which is not an attribute of 'account.CustomUser'.
<class 'account.admin.UserAd'>: (admin.E019) The value of 'filter_horizontal[1]' refers to 'user_permissions', which is not an attribute of 'account.CustomUser'.
<class 'account.admin.UserAd'>: (admin.E031) The value of 'ordering' must be a list or tuple.
<class 'account.admin.UserAd'>: (admin.E116) The value of 'list_filter[3]' refers to 'groups', which does not refer to a Field.

Error 1: <class 'account.admin.UserAd'>: (admin.E019) The value of 'filter_horizontal[0]' refers to 'groups', which is not an attribute of 'account.CustomUser'.

Suggestion 1: It looks like you are trying to filter on the groups a user belongs to however because groups are not an attribute of the CustomUser model class the admin is failing.

Error 2: <class 'account.admin.UserAd'>: (admin.E019) The value of 'filter_horizontal[1]' refers to 'user_permissions', which is not an attribute of 'account.CustomUser'.

Suggestion 2: Same as the first suggestion. Your code is referencing an attribute of the CustomUser class which doesn't exist.

Error 3: <class 'account.admin.UserAd'>: (admin.E031) The value of 'ordering' must be a list or tuple.

Suggestion 3: Single value tuples need to be followed with a comma even though the tuple only includes a single value. Should instead be: ordering = ("username",)

Error 4: <class 'account.admin.UserAd'>: (admin.E116) The value of 'list_filter[3]' refers to 'groups', which does not refer to a Field.

Suggestion 4: Same as the first suggestion. Your code is referencing an attribute of the CustomUser class which doesn't exist.

It looks like this other poster had a similar requirement to you. Perhaps their solution would work for you: In Django admin, how to filter users by group?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM