Я работаю над проектом Django, в котором определил модель CustomUser в приложении под названием аутентификация. Я правильно установил AUTH_USER_MODEL для ссылки на эту модель, но когда я пытаюсь выполнить миграцию, я получаю следующие ошибки:
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authentication.Device.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authentication.MoodReview.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authentication.PasswordResetCode.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authtoken.Token.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
# authentication/models.py
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.db import models
from django.utils import timezone
class CustomUserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
if not email:
raise ValueError("The Email field must be set")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self.create_user(email, password, **extra_fields)
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
name = models.CharField(max_length=255)
# ... other fields
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
def __str__(self):
return self.email
**Обновлена конфигурация
**
AUTH_USER_MODEL = 'authentication.CustomUser'
**INSTALLED_APPS
**
INSTALLED_APPS = [
'authentication',
'django.contrib.admin',
'django.contrib.auth',
# ... other apps
]
Подробнее здесь: https://stackoverflow.com/questions/790 ... -migration
Модель Django CustomUser вызывает ошибки ленивой ссылки во время миграции ⇐ Python
Программы на Python
1727418122
Anonymous
Я работаю над проектом Django, в котором определил модель CustomUser в приложении под названием аутентификация. Я правильно установил AUTH_USER_MODEL для ссылки на эту модель, но когда я пытаюсь выполнить миграцию, я получаю следующие ошибки:
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authentication.Device.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authentication.MoodReview.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authentication.PasswordResetCode.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
The field authtoken.Token.user was declared with a lazy reference to 'authentication.customuser', but app 'authentication' doesn't provide model 'customuser'.
# authentication/models.py
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
from django.db import models
from django.utils import timezone
class CustomUserManager(BaseUserManager):
def create_user(self, email, password=None, **extra_fields):
if not email:
raise ValueError("The Email field must be set")
email = self.normalize_email(email)
user = self.model(email=email, **extra_fields)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self.create_user(email, password, **extra_fields)
class CustomUser(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(unique=True)
name = models.CharField(max_length=255)
# ... other fields
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
objects = CustomUserManager()
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['name']
def __str__(self):
return self.email
**Обновлена конфигурация
**
AUTH_USER_MODEL = 'authentication.CustomUser'
**INSTALLED_APPS
**
INSTALLED_APPS = [
'authentication',
'django.contrib.admin',
'django.contrib.auth',
# ... other apps
]
Подробнее здесь: [url]https://stackoverflow.com/questions/79029993/django-customuser-model-causes-lazy-reference-errors-during-migration[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия