Django1.8.6lookuperror:应用程序没有用户mod

2024-03-29 10:04:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在appmain中创建自定义用户模型,并将其用于管理以及python社交身份验证。 当我运行第一次迁移时,一切都很好,但是如果我尝试运行管理.py再次迁移它抛出应用程序找不到用户模型。在

我已经配置了: 1已在中安装应用程序设置.py 2在AUTH_user_模型和SOCIAL_AUTH_user_模型中设置用户模型

以下是我的代码: 主要/型号

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'

主/管理员.py在

^{pr2}$

在设置.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

Tags: djangoinpyselftruemodelismodels
2条回答

我不确定social-auth,但对于一些auth应用程序,它们在^{中的顺序很重要。别忘了运行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',
)

固定的: 不幸的是,这与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 !

相关问题 更多 >