(admin.E116)“ list_filter [3]”的值引用“组”,而不引用字段

时间:2019-05-04 01:19:15

标签: django django-models django-testing

尝试创建我自己的用户模型和admin时,该测试出现以下错误。

  

:(管理员E019)“ filter_horizo​​ntal [0]”的值表示“组”,而不是“ account.User”的属性。   :(admin.E019)“ filter_horizo​​ntal [1]”的值表示“ user_permissions”,而不是“ account.User”的属性。   :(admin.E116)'list_filter [1]'的值引用'is_superuser',它不引用字段。   :(admin.E116)“ list_filter [3]”的值引用“组”,而不引用字段。

models.py

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

class UserManager(BaseUserManager):
    def create_user(self,username,email,password=None, **extra_fields):
        user=self.model(
            username=username.lower(),
            email=self.normalize_email(email),
            **extra_fields)
        #user name is converted into lowercase
        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
        )
        user.set_password(password)
        user.is_superuser=True
        user.is_staff =True
        user.save(using=self._db)

        return user
class User(AbstractBaseUser):
    username=models.CharField(max_length=255,unique=True)
    email=models.EmailField(max_length=255,unique=True)
    password=models.CharField(max_length=255)


    is_staff =models.BooleanField(default=False)
    is_active=models.BooleanField(default=True)

    objects=UserManager()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']

admin.py

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

# Register your models here.''''

class UserAdmin(BaseUserAdmin):
    ordering = ['id']
    list_display=['username','email']


admin.site.register(models.User,UserAdmin)

admintest.py

from django.test import TestCase, Client
from django.contrib.auth import get_user_model
from django.urls import reverse

class AdminTests(TestCase):
    def setUp(self):
        self.client = Client()

        self.admin_user = get_user_model().objects.create_superuser(
            username='admin',
            password='123',
            email='fda@gmail.com'
        )
        self.client.force_login(self.admin_user)
        self.user = get_user_model().objects.create_user(
            username='mantis',
            password='123',
            email='111'
        )

    def test_users_listed(self):
        """Test that users are listed on the user page"""
        url = reverse('admin:core_user_changelist')
        res = self.client.get(url)

        self.assertContains(res, self.user.username)
        self.assertContains(res, self.user.email)

2 个答案:

答案 0 :(得分:1)

您只需要在PermissionsMixin中添加class User(AbstractBaseUser),这样User(AbstractBaseUser, PermissionsMixin) 您可以使用以下命令导入它:from django.contrib.auth.models import PermissionsMixin

答案 1 :(得分:0)

如果你查看 django 文档 https://docs.djangoproject.com/en/3.1/ref/contrib/admin/#module-django.contrib.admin

在注释中,他们说您需要将自定义字段添加到字段集(用于编辑用户的字段)和 add_fieldsets(用于创建用户时使用的字段)。

因此您需要将这些字段添加到您的 UserAdmin 类中