2016-12-13 332 views
0

我無法在admin.py中註冊自定義用戶模型。沒有admin.py,所有工作正常django AbstractUser model'str'object has no attribute'_meta'

爲什麼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' 

回答

1

此行導入您的用戶模型的字符串名稱,而不是實際的班級。

from settings import AUTH_USER_MODEL as CustomUser 

您應該從您的models.py文件中導入該類。

from <app_name>.models import User as CustomUser 
相關問題