2015-11-13 14 views
7

Tôi đang cố cài đặt django-registration-redux bằng customUser.Tại sao "Mô hình chưa được tải"?

Tôi đã bao gồm này trong settings.py của tôi:

AUTH_USER_MODEL = 'app.customUser' 

Mẫu đăng ký là một trong ../registration/forms.py thư mục:

from __future__ import unicode_literals 


from django import forms 
from django.utils.translation import ugettext_lazy as _ 
from django.contrib.auth.forms import UserCreationForm 

from .users import UserModel, UsernameField 

User = UserModel() 


class RegistrationForm(UserCreationForm): 

    required_css_class = 'required' 
    email = forms.EmailField(label=_("E-mail")) 

    class Meta: 
     model = customUser 
     fields = ('email') 

Bên cạnh đó, models.py có sau:

from __future__ import unicode_literals 
from django.db import models 

# Create your models here. 

import datetime 
import hashlib 
import random 
import re 

from django.conf import settings 
from django.core.mail import EmailMultiAlternatives 
from django.db import models 
from django.template import RequestContext, TemplateDoesNotExist 
from django.template.loader import render_to_string 
from django.utils.translation import ugettext_lazy as _ 
from django.utils.encoding import python_2_unicode_compatible 
from django.utils.timezone import now as datetime_now 
from django.utils import six 

from .users import UserModel, UserModelString 


from django.contrib.auth.models import BaseUserManager, AbstractBaseUser 
from django.contrib.auth.admin import UserAdmin 


class customUserManager(BaseUserManager): 
    def create_user(self, email, password=None): 
     """ 
     Creates and saves a User with the given email, date of 
    birth and password. 
     """ 
     if not email: 
      raise ValueError('Users must have an email address') 

     user = self.model(
      email=self.normalize_email(email), 
     ) 

     user.set_password(password) 
     user.save(using=self._db) 
     return user 

    def create_superuser(self, email, password): 
     """ 
     Creates and saves a superuser with the given email, date of 
    birth and password. 
     """ 
     user = self.create_user(email, 
      password=password, 
     ) 
     user.is_active = True 
     user.is_admin = True 
     user.save(using=self._db) 
     return user 

class customUser(AbstractBaseUser): 
    email = models.EmailField(verbose_name='email address', max_length=255, unique=True) 
    first_name = models.CharField(max_length=50, null=True) 
    middle_name = models.CharField(max_length=50, null=True) 
    last_name = models.CharField(max_length=50, null=True) 
    is_active = models.BooleanField(default=False) 
    is_admin = models.BooleanField(default=False) 

    objects = customUserManager() 

    USERNAME_FIELD = 'email' 


class customUserAdmin(UserAdmin): 
    # The forms to add and change user instances 
    #form = UserChangeForm 
    #add_form = RegisterationForm 

class RegistrationManager(models.Manager): 
    """ 
    """ 

đây là traceback:

0.123.
Traceback (most recent call last): 
    File "manage.py", line 10, in <module> 
    execute_from_command_line(sys.argv) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line 
utility.execute() 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/core/management/__init__.py", line 312, in execute 
django.setup() 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/__init__.py", line 18, in setup 
apps.populate(settings.INSTALLED_APPS) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/apps/registry.py", line 108, in populate 
app_config.import_models(all_models) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/apps/config.py", line 198, in import_models 
self.models_module = import_module(models_module_name) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module 
__import__(name) 
    File "/Users/Desktop/app/sites/models.py", line 21, in <module> 
from .users import UserModel, UserModelString 
    File "/Users/Desktop/app/sites/users.py", line 4, in <module> 
UserModel = get_user_model() 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 150, in get_user_model 
return django_apps.get_model(settings.AUTH_USER_MODEL) 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/apps/registry.py", line 199, in get_model 
self.check_models_ready() 
    File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/apps/registry.py", line 131, in check_models_ready 
raise AppRegistryNotReady("Models aren't loaded yet.") 
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. 

Nếu tôi loại bỏ các() trong get_user_model trong users.py của tôi, tôi nhận được lỗi này:

users.py

from django.conf import settings 
from django.contrib.auth import get_user_model 

UserModel = get_user_model 

def UserModelString(): 
    try: 
     return settings.AUTH_USER_MODEL 
    except AttributeError: 
     return 'auth.User' 


def UsernameField(): 
    return getattr(UserModel(), 'USERNAME_FIELD', 'email') 

Lỗi:

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/django/contrib/auth/__init__.py", line 155, in get_user_model 
"AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL 
django.core.exceptions.ImproperlyConfigured: AUTH_USER_MODEL refers to model 'app.customUser' that has not been installed 
+0

bạn đã xác định 'RegistrationForm' ở đâu? Hiển thị cho chúng tôi 'app/sites/models.py'. – ozgur

+0

Đây là: cho tôi biết bạn cần gì khác. –

Trả lời

7

Đây là tài liệu về chức năng get_user_model có thể hữu ích.

Generally speaking, you should reference the User model with the AUTH_USER_MODEL setting in code that is executed at import time. get_user_model() only works once Django has imported all models.

Vấn đề là trong tập tin ../registration/forms.py:

User = UserModel() 

UserModel được thực hiện trong thời gian nhập khẩu, đó là sai và ném ngoại lệ 'Mô hình không được nạp chưa'. Sử dụng UserModel() trực tiếp bên trong các phương thức/chức năng của bạn khi cần. Xem một vài ví dụ: here.

Hy vọng điều này sẽ hữu ích.

+2

Kể từ Django 1,11, ["Khả năng gọi get_user_model() tại thời điểm nhập đã được thêm."] (Https://docs.djangoproject.com/en/1.11/topics/auth/customizing) – bsapaka

Các vấn đề liên quan