Nefonfo
Nefonfo

Reputation: 35

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

Hi I was making a wagtail app but I have a problem overriding the user model with the abstract user and the BaseUserManager, trying to make migrations but it crashes and show this error on console:

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

It uses a package from django_countries And a choice to the account type

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'

Upvotes: 1

Views: 1207

Answers (1)

gasman
gasman

Reputation: 25237

As per the Wagtail docs, a custom user model must at minimum inherit from AbstractBaseUser and PermissionsMixin - the latter provides the necessary m2m relation to Group. The simple way to do this is to inherit from Django's AbstractUser, but you can inherit them individually too:

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin

class Account(AbstractBaseUser, PermissionsMixin):

Upvotes: 2

Related Questions