2016-12-13 4 views
0

admin.py에서 사용자 정의 사용자 모델을 등록 할 수 없습니다. admin.py는 작동하지 않습니다. 모두 정상적으로 작동합니다.django Abstract 사용자 모델 'str'객체에 '_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