django AbstractUser 模型 'str' 对象没有属性 '_meta'

django AbstractUser model 'str' object has no attribute '_meta'

我无法在 admin.py 中注册自定义用户模型。没有 admin.py ,一切正常

为什么要自定义用户?我不知道这是什么问题。我按照文档做了一切

models.py

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils.translation import ugettext_lazy as _

AbstractUser._meta.get_field('email')._unique = True
AbstractUser._meta.get_field('email').blank = False


class User(AbstractUser):
    is_agency = models.BooleanField(_('agency status'), default=False)
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = ['username']

    def get_is_agency(self):
        return self.is_agency

admin.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from settings import AUTH_USER_MODEL as CustomUser

admin.site.register(CustomUser, UserAdmin)

回溯

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f169c48a730>
Traceback (most recent call last):
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 113, in inner_run
    autoreload.raise_last_exception()
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 249, in raise_last_exception
    six.reraise(*_exception)
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/utils/autoreload.py", line 226, in wrapper
    fn(*args, **kwargs)
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/__init__.py", line 27, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/contrib/admin/apps.py", line 23, in ready
    self.module.autodiscover()
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/home/ti/PycharmProjects/venv/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/ti/PycharmProjects/reklama/user/admin.py", line 32, in <module>
    admin.site.register(CustomUser, UserAdmin)
  File "/home/ti/PycharmProjects/venv/lib/python3.5/site-packages/django/contrib/admin/sites.py", line 87, in register
    if model._meta.abstract:
AttributeError: 'str' object has no attribute '_meta'

此行导入的是您的用户模型的字符串名称,而不是实际的 class。

from settings import AUTH_USER_MODEL as CustomUser

您应该从 models.py 文件中导入 class。

from <app_name>.models import User as CustomUser

我可以想到两个可能的原因:

1) 如果你使用这个 admin.site.register(classname)(带有 single/double 引号)它会生成错误

改用这个:

admin.site.register(**classname**)  # (without single/double quotes)

2) 如果在admin.py中忘记导入class,也会产生同样的错误。

要保持​​应用程序的独立性,您应该使用:

from django.contrib.auth import get_user_model

CustomUser = get_user_model()

您可以查看它的作用here