django.core.exceptions.FieldError:为帐户指定了未知字段(组)

问题描述 投票:0回答:1

[嗨,我当时正在制作一个ag应用程序,但我遇到了一个问题,即用抽象用户和BaseUserManager覆盖用户模型,试图进行迁移,但崩溃了,并在控制台上显示此错误:

django.core.exceptions.FieldError: Unknown field(s) (groups) specified for Account

它使用django_countries中的软件包并选择帐户类型

Models.py

from django.db import models
from django_countries.fields import CountryField
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager

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

        user = self.model(
            email=self.normalize_email(email),
            username=username,
            country=country
        )

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

    def create_superuser(self, email, username, country, password):
        user = self.create_user(
            email=self.normalize_email(email),
            password=password,
            username=username,
            country=country,
            account_type='MTR'
        )
        user.is_admin = True
        user.is_staff = True
        user.is_superuser = True
        user.save(using=self._db)
        return user


class Account(AbstractBaseUser):
    class AccountTypes(models.TextChoices):
        MASTER = 'MTR', _('Master')
        ADMINISTRATOR = 'ADM', _('Administrator')
        MODERATOR = 'MOD', _('Moderator')
        USER = 'USR', _('User')

    email = models.EmailField(
        verbose_name="email", max_length=60, unique=True, null=False, blank=False)
    username = models.CharField(
        max_length=30, unique=True, null=False, blank=False)
    photo = models.ImageField(
        verbose_name='photo', upload_to='profile_photos', null=True, blank=True)
    country = CountryField()
    account_type = models.CharField(max_length = 3, choices = AccountTypes.choices, default = AccountTypes.USER, null = False, blank = False)
    date_joined = models.DateTimeField(
        verbose_name='date joined', auto_now_add=True)
    last_login = models.DateTimeField(_('last login'), blank=True, null=True)
    is_admin = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    is_superuser = models.BooleanField(default=False)

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['email', 'username', 'country', 'account_type']
    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

settings / base.py

INSTALLED_APPS = [
'django_countries',
'account',
'home',
'search',

'wagtail.contrib.forms',
'wagtail.contrib.redirects',
'wagtail.embeds',
'wagtail.sites',
'wagtail.users',
'wagtail.snippets',
'wagtail.documents',
'wagtail.images',
'wagtail.search',
'wagtail.admin',
'wagtail.core',

'modelcluster',
'taggit',

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]

AUTH_USER_MODEL = 'account.Account'
django wagtail
1个回答
0
投票
As per the Wagtail docs,自定义用户模型必须至少继承自AbstractBaseUserPermissionsMixin-后者提供与Group必要的m2m关系。一种简单的方法是从Django的AbstractUser继承,但是您也可以单独继承它们:

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin class Account(AbstractBaseUser, PermissionsMixin):

© www.soinside.com 2019 - 2024. All rights reserved.