привет, я создаю собственный класс менеджера под названием usermanager в monage.py, чтобы настроить мой шаблон входа для 2 типов пользователей-администраторов, предпринимателей
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
from django.contrib.auth.models import BaseUserManager
import sys
def main():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'oasisconsuting.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)
if __name__ == '__main__':
main()
class UserManager(BaseUserManager):
use_in_migrations = True
def _create_user(self, email, password, **extra_fields):
if not email:
raise ValueError("The given email 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_user(self, email, password=None, **extra_fields):
extra_fields.setdefault("is_staff", False)
extra_fields.setdefault("is_superuser", False)
return self._create_user(email, password, **extra_fields)
def create_superuser(self, email, password, **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)
после запуска makemigration (это нормально), когда я запускаю код миграции мы получаем ошибки, что не так в этом коде, чтобы показать мне эти ошибки:
C:\Users\hp\PycharmProjects\business\Scripts\python.exe
C:/Users/hp/PycharmProjects/business/oasisconsuting/manage.py runserver
Traceback (most recent call last):
File "C:/Users/hp/PycharmProjects/business/oasisconsuting/manage.py", line 4, in <module>
from django.contrib.auth.models import BaseUserManager
File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\contrib\auth\models.py", line
2, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\contrib\auth\base_user.py",
line 47, in <module>
class AbstractBaseUser(models.Model):
File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\db\models\base.py", line 107,
in __new__
app_config = apps.get_containing_app_config(module)
File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\apps\registry.py", line 252, in
get_containing_app_config
self.check_apps_ready()
File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\apps\registry.py", line 134, in
check_apps_ready
settings.INSTALLED_APPS
File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\conf\__init__.py", line 76, in
__getattr__
self._setup(name)
File "C:\Users\hp\PycharmProjects\business\lib\site-packages\django\conf\__init__.py", line 57, in
_setup
raise ImproperlyConfigured(
django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not
configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call
settings.configure() before accessing settings.
следует ли мне сохранить этот код или изменить его, как я могу
- вызовите setting.configure () или
- , определите переменную среды DJANGO_SETTINGS_MODULE, чтобы исправить мою проблему, или есть другой способ сделать