我无法在admin.py中注册自定义用户模型。没有admin.py,一切正常
为什么使用customUser?我不知道这是什么问题。我根据文档做了一切
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'
答案 0 :(得分:3)
此行导入用户模型的字符串名称,而不是实际的类。
from settings import AUTH_USER_MODEL as CustomUser
您应该从models.py
文件中导入该类。
from <app_name>.models import User as CustomUser
答案 1 :(得分:1)
我可以想到两个可能的原因:
1)
如果你用这个
admin.site.register(classname)
(带有单引号/双引号)会产生错误
代替使用此:
admin.site.register(**classname**) # (without single/double quotes)
2)
如果您忘记在admin.py
中导入 class ,则会生成相同的错误。
答案 2 :(得分:0)
要保持应用程序独立,您应该使用:
from django.contrib.auth import get_user_model
CustomUser = get_user_model()
您可以检查它的作用here