Привет, я делал приложение для трясогузки, но у меня проблема с переопределением пользовательской модели с помощью абстрактного пользователя и 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'