在settings.py文件夹内的应用中设置AUTH_USER_MODEL

时间:2018-09-10 03:41:16

标签: python django django-authentication

我是django的新手,我试图在我的一个应用程序中扩展用户类,但是我无法从set.py重定向到包含AbstractUser的模型

我的Nproject:

  -apps:
   --profile:
    ---__init__.py
    ---apps.py
    ---forms.py
    ---models.py
    ---urls.py
    ---views.py
   --apps1
 -media 
 -static    
 -templates 
 -Nproject:
  --__init__.py
  --settings.py
  --urls.py
  --wsgi.py

apps.profile.models中的模型

from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    address = models.CharField(max_length=255)
    def __str__(self):
        return '%s %s' % (self.email, self.username)

setting.py,我认为这是我的错误

AUTH_USER_MODEL = 'apps.User'   #not working

我认为错误在于AUTH_USER_MODEL,我不知道如何正确引用模型用户

研究一些站点,并告诉我应该使用 init .py文件,但该文件不起作用

在apps.profile中

初始化

from .models import User

错误:

Traceback (most recent call last):                                                                                                                          
  File "./manage.py", line 22, in <module>                                                                                                                  
    execute_from_command_line(sys.argv)                                                                                                                     
  File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line            
    utility.execute()                                                                                                                                       
  File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/core/management/__init__.py", line 347, in execute                              
    django.setup()                                                                                                                                          
  File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/__init__.py", line 24, in setup                                                 
    apps.populate(settings.INSTALLED_APPS)                                                                                                                  
  File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate                                         
    app_config = AppConfig.create(entry)                                                                                                                    
  File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "/home/projects/.virtualenvs/Nproject/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/home/projects/Nproject/apps/perfil/__init__.py", line 1, in <module>
    from .models import User
  File "/home/projects/Nproject/apps/perfil/models.py", line 2, in <module>
    from django.contrib.auth.models import User
  File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/contrib/auth/models.py", line 2, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/projects/.virtualenvs/Nprojects/lib/python3.6/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/db/models/base.py", line 100, in __new__
    app_config = apps.get_containing_app_config(module)
  File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/apps/registry.py", line 244, in get_containing_app_config
    self.check_apps_ready()
  File "/home/projects/.virtualenvs/Nproject/lib/python3.6/site-packages/django/apps/registry.py", line 127, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.

其他数据: -Django 2.1 -Python 3.6.5

1 个答案:

答案 0 :(得分:1)

AUTH_USER_MODEL的正确名称字符串应为appname.modelname。在您的情况下,我想它是'profile.user'(如果您自己更改应用名称,可能会有所不同)