Django 1.8.6 LookupError:App没有用户模型

时间:2015-11-21 14:45:47

标签: python django

我在app main中创建自定义用户模型,并使用相同的admin和python social auth。 当我运行我的第一次迁移时,一切运行良好,但是如果我再次尝试运行manage.py migrate,则会抛出应用程序无法找到用户模型。

我已配置: 1.在settings.py中安装了应用程序 2.在AUTH_USER_MODEL和SOCIAL_AUTH_USER_MODEL中设置用户模型

以下是我的代码: 主/模型

from django.db import models

# Create your models here.
from django.contrib.auth.models import AbstractBaseUser,BaseUserManager,PermissionsMixin
from django.utils import timezone
from django.core import validators
from django.utils.translation import ugettext_lazy as _
from django.conf import settings

class CustomUserManager(BaseUserManager):

    def _create_user(self, email,username, password,
                     is_staff, is_superuser, **extra_fields):
        """
        Creates and saves a User with the given username, email and password.
        """
        now = timezone.now()
        if not email:
            raise ValueError('The given username must be set')
        email = self.normalize_email(email)
        user = self.model(email=email,username=username,
                          is_staff=is_staff, is_active=True,
                          is_superuser=is_superuser, last_login=now,
                          date_joined=now, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_user(self, username, email=None, password=None, **extra_fields):
        now = timezone.now()
        if not email:
            raise ValueError('The given username must be set')
        email = self.normalize_email(email)
        user = self.model(email=email,username=username,
                          is_staff=False, is_active=True,
                          is_superuser=False, last_login=now,
                          date_joined=now, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, username, email, password=None, **extra_fields):
        now = timezone.now()
        if not email:
            raise ValueError('The given username must be set')
        email = self.normalize_email(email)
        user = self.model(email=email,username=username,
                          is_staff=True, is_active=True,
                          is_superuser=True, last_login=now,
                          date_joined=now, **extra_fields)
        user.set_password(password)
        user.save(using=self._db)
        return user

    class Meta:
        app_label = 'main'


class myuser(AbstractBaseUser,PermissionsMixin):

    GENDERS = (
        ('male', 'Male'),
        ('female', 'Female')
    )
    VISIBLE = (
        ('public', 'public'),
        ('private', 'private')
    )
    username = models.CharField(_('username'), max_length=30, unique=True,help_text=_('Required. 30 characters or fewer. Letters, digits and '
                    '@/./+/-/_ only.'),
        validators=[
            validators.RegexValidator(r'^[\w.@+-]+$', _('Enter a valid username.'), 'invalid')
        ])

    first_name = models.CharField(max_length=30, blank=True)
    last_name = models.CharField(max_length=30, blank=True)
    email = models.EmailField(blank=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(_('active'), default=True)
    date_joined = models.DateTimeField(default=timezone.now)

    gender = models.CharField(max_length=20, null=True, blank=True,
                              choices=GENDERS)
    visibility = models.CharField(max_length=20, null=True, blank=True,
                              choices=VISIBLE,default='public')
    birthday = models.DateField(default=timezone.now)
    facebook_id = models.CharField(max_length=30, blank=True)
    city = models.CharField(max_length=250, null=True, blank=True)
    dob = models.DateField(blank=True, null=True)
    locale = models.CharField(max_length=10, blank=True, null=True)
    height = models.IntegerField(blank=True,null=True)
    weight = models.IntegerField(blank=True,null=True)
    profile_photo = models.ImageField(upload_to=settings.STATIC_ROOT+'profiles/',null=True)

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = ['email']
    objects = CustomUserManager()

    def __unicode__(self):
        return self.username

    def save(self, *args, **kwargs):
        """ ensure instance has usable password when created """
        if not self.pk and self.has_usable_password() is False:
            self.set_password(self.password)

        super(myuser, self).save(*args, **kwargs)
    def get_gender(self):
        "Returns the short name for the user."
        return self.gender
    def get_short_name(self):
        "Returns the short name of user"
        return self.first_name

    def get_profile_photo(self):
        "Returns the short name of user"
        return self.profile_photo

    class Meta:
        app_label = 'main'

主/ admins.py

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import Group
from django.contrib.auth.forms import UserChangeForm,UserChangeForm, UserCreationForm
from main.models import myuser
from django import forms
from django.utils.translation import gettext as _

class MyUserChangeForm(UserChangeForm):
    class Meta(UserChangeForm.Meta):
        model = myuser

class MyUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm.Meta):
        model = myuser

    def clean_username(self):
        username = self.cleaned_data['username']
        try:
            myuser.objects.get(username=username)
        except myuser.DoesNotExist:
            return username
        raise forms.ValidationError(self.error_messages['duplicate_username'])

class MyUserAdmin(UserAdmin):
    form = MyUserChangeForm
    add_form = MyUserCreationForm
    fieldsets = (
        (None, {'fields': ('username', 'password')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email','birthday','gender','city','visibility')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'password1', 'password2')}
        ),
    )
    list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')
    list_filter = ('is_staff', 'is_superuser', 'is_active', 'groups')
    search_fields = ('username', 'first_name', 'last_name', 'email')
    ordering = ('username',)
    filter_horizontal = ('groups', 'user_permissions',)


admin.site.register(myuser, MyUserAdmin)
admin.site.unregister(Group)

settings.py

INSTALLED_APPS = (
    'grappelli',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'main',
    'registration',
    'profiles',
    'crispy_forms',
    'social.apps.django_app.default',
)

AUTHENTICATION_BACKENDS = (
   'social.backends.facebook.FacebookOAuth2',
   'social.backends.google.GoogleOAuth2',
   'social.backends.twitter.TwitterOAuth',
   'social.backends.instagram.InstagramOAuth2',
   'social.backends.fitbit.FitbitOAuth',
   'django.contrib.auth.backends.ModelBackend',
)

AUTH_USER_MODEL = 'main.myuser'

SOCIAL_AUTH_USER_MODEL = 'main.myuser'

错误日志:

# python manage.py migrate

/var/webapp/fit_env/lib/python3.4/site-packages/django/contrib/sites/models.py:78: RemovedInDjango19Warning: Model class django.contrib.sites.models.Site doesn't declare an explicit app_label and either isn't in an application in INSTALLED_APPS or else was imported before its application was loaded. This will no longer be supported in Django 1.9.
  class Site(models.Model):

Operations to perform:
  Synchronize unmigrated apps: messages, staticfiles, crispy_forms, grappelli
  Apply all migrations: sessions, auth, default, registration, contenttypes, main, admin
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states...Traceback (most recent call last):
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/apps/config.py", line 159, in get_model
    return self.models[model_name.lower()]
KeyError: 'user'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/db/migrations/state.py", line 238, in __init__
    model = self.get_model(lookup_model[0], lookup_model[1])
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/apps/registry.py", line 202, in get_model
    return self.get_app_config(app_label).get_model(model_name.lower())
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/apps/config.py", line 162, in get_model
    "App '%s' doesn't have a '%s' model." % (self.label, model_name))
LookupError: App 'main' doesn't have a 'user' model.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 346, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/db/migrations/executor.py", line 100, in migrate
    state.apps  # Render all real_apps -- performance critical
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/utils/functional.py", line 59, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/db/migrations/state.py", line 166, in apps
    return StateApps(self.real_apps, self.models)
  File "/var/webapp/fit_env/lib/python3.4/site-packages/django/db/migrations/state.py", line 248, in __init__
    raise ValueError(msg.format(field=operations[0][1], model=lookup_model))
ValueError: Lookup failed for model referenced by field default.UserSocialAuth.user: main.User

2 个答案:

答案 0 :(得分:2)

固定: 不幸的是,这与django设置或迁移无关。 我可能没有选项(--no-site-packages)创建了我的虚拟环境 我认为这就是python-social-auth一直引用默认用户(不知道为什么)的原因。

解决方案:

Removed virtual env.
Created new virtual env. with --no-site-packages
Installed required packages in this env. and voila its working fine now !

答案 1 :(得分:1)

我不确定social-auth,但对于某些auth个应用,他们在INSTALLED_APPS的订单很重要。另外,不要忘记运行python manage.py makemigrations

INSTALLED_APPS = (
    'grappelli',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'social.apps.django_app.default',
    'crispy_forms',
    'registration',
    'profiles',
    'main',
)