【问题标题】:Django 1.8 LookupError AUTH_USER_MODELDjango 1.8 LookupError AUTH_USER_MODEL
【发布时间】:2015-07-28 00:29:11
【问题描述】:

我在名为fowl 的应用程序中使用了自定义用户模型。 当我运行syncdbmakemigrationsmigrate 时,我得到一个LookupError。请帮忙

settings.py 中,我将AUTH_USER_MODEL 定义为'fowl.User'

fowl/models.py

    from django.db import models
    from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager
    from django.utils import timezone
    from django.core.mail import send_mail
    from django.utils.translation import ugettext_lazy as _

class UserManager(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_admin = True
        user.save(using=self._db)
        return user


class User(AbstractBaseUser, PermissionsMixin):
    """
    Custom user class.
    """
    email = models.EmailField(_('email address'), unique=True, db_index=True)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)
    is_staff = models.BooleanField(_('staff status'), default=False)
    date_joined = models.DateTimeField(default=timezone.now)
    first_name = models.CharField(_('first name'), max_length=30, blank=True)
    last_name = models.CharField(_('last name'), max_length=30, blank=True)
    is_festival = models.BooleanField(default=True)

    objects = UserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def __unicode__(self):
        return self.email

    class Meta:
        verbose_name = _('user')
        verbose_name_plural = _('users')
        abstract = True

    def get_full_name(self):
        """
        Returns the first_name plus the last_name, with a space in between.
        """
        full_name = '%s %s' % (self.first_name, self.last_name)
        return full_name.strip()

    def get_short_name(self):
        """
        Returns the short name for the user.
        """
        return self.first_name

    def email_user(self, subject, message, from_email=None, **kwargs):
        """
        Sends an email to this User.
        """
        send_mail(subject, message, from_email, [self.email], **kwargs)

    @property
    def is_festival(self):
        """Is the user a member of staff?"""
        return self.is_festival

当我运行syncdbmakemigrations 时,我得到一个LookupError

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/Blu/projects/fowl/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Users/Blu/projects/fowl/env/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/Blu/projects/fowl/env/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/Blu/projects/fowl/env/lib/python2.7/site-packages/django/core/management/base.py", line 440, in execute
    self.check()
  File "/Users/Blu/projects/fowl/env/lib/python2.7/site-packages/django/core/management/base.py", line 478, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/Blu/projects/fowl/env/lib/python2.7/site-packages/django/core/checks/registry.py", line 72, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/Blu/projects/fowl/env/lib/python2.7/site-packages/django/contrib/auth/checks.py", line 12, in check_user_model
    cls = apps.get_model(settings.AUTH_USER_MODEL)
  File "/Users/Blu/projects/fowl/env/lib/python2.7/site-packages/django/apps/registry.py", line 202, in get_model
    return self.get_app_config(app_label).get_model(model_name.lower())
  File "/Users/Blu/projects/fowl/env/lib/python2.7/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 'fowl' doesn't have a 'user' model.

我做错了什么?

【问题讨论】:

  • 你跑manage.py migrate了吗?
  • 好问题 Eva,也别忘了manage.py makemigrations
  • @Eva611 是的。我仍然得到同样的错误
  • @nir 谢谢。我跑了两者并得到相同的LookupError。如果我尝试将它们添加到 admin.py 我会得到一个 django.core.exceptions.ImproperlyConfigured: The model User is abstract, so it cannot be registered with admin.
  • 在你的class Meta - 你有abstract=True。删除它,然后运行 ​​makemigrations

标签: python django django-models django-authentication django-1.8


【解决方案1】:

在您的User 类'Meta 中,您设置了导致问题的abstract=True。删除它,然后运行makemigrations

另外,请注意,您正在重复AbstractUser class 中已经存在的许多字段。你可以消灭它们。

class User(AbstractBaseUser, PermissionsMixin):
    is_admin = models.BooleanField(default=False)
    is_festival = models.BooleanField(default=True)

    #The properties, methods, etc.. 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 2021-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多