Django 1.8 LookupError AUTH_USER_MODEL

六眼飞鱼酱① 提交于 2019-12-21 05:16:06

问题


I'm using a custon user model as such in my app called fowl. When I run syncdb or makemigrations or migrate I get a LookupError. Please help

In settings.py I have defined AUTH_USER_MODEL as '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

When I run syncdb or makemigrations I get a 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.

What Am I doing wrong?


回答1:


In your User class' Meta, you have set the abstract=True which is causing the issue. Remove it and then run makemigrations.

Also, note that you are repeating many fields already present in AbstractUser class. You may eliminate them.

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

    #The properties, methods, etc.. 


来源:https://stackoverflow.com/questions/30270381/django-1-8-lookuperror-auth-user-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!