Django的。用户模型的问题

时间:2015-06-28 12:58:32

标签: django

从django 1.6到django 1.8更新项目时遇到问题

我有模特个人资料

class Profile(AbstractUser):
    middle_name = models.CharField(max_length=70, blank=True, verbose_name=_("Middle name"))
    gender = models.CharField(max_length=2, choices=GENDER_CHOICES)
    phone = models.CharField(max_length=150, blank=True, verbose_name=_("Phone"))
    status = models.CharField(max_length=500, blank=True, verbose_name=_("Status"))
    education = models.CharField(max_length=350, blank=True, verbose_name=_("Education"))
    progress_display = models.BooleanField(default=True, blank=True, verbose_name=_("Display progress"))
    about = models.TextField(blank=True, verbose_name=_("description about"))
    city = models.CharField(max_length=75, blank=True, verbose_name=_("City"))
    current_city = models.CharField(max_length=75, blank=True, verbose_name=_("Current city"))
    birth = models.DateTimeField(blank=True, null=True, verbose_name=_("Birthday"))
    registered = models.DateTimeField(auto_now=True, help_text=_('Registration date'),
                                      verbose_name=_("registration date"))
    is_teacher = models.BooleanField(default=False, blank=True, verbose_name=_("is_teacher"))
    hide_fields = models.CharField(max_length=255, blank=True)
    subscription = models.BooleanField(default=False, blank=True, verbose_name='Подписка на рассылку')
    main_photo = models.ForeignKey('avatar.Photo', blank=True, verbose_name=_("user main-photo"), related_name='avatar', default=None, null=True)

    class Meta:
        verbose_name = _('User')
        verbose_name_plural = _('Users')
        ordering = ('-registered', 'id')
        get_latest_by = 'registered'

我的应用程序位于单独的文件夹(“应用程序”)中。我在以下设置中定义的文件夹路径:

BASE_DIR = os.path.abspath(os.path.dirname(__file__))
sys.path.append(os.path.join(BASE_DIR, 'applications'))

我还在设置中定义了自定义用户模型(位于“个人资料”应用程序中)

AUTH_USER_MODEL = 'profile.Profile'

这是我的安装应用:

INSTALLED_APPS = (
    'social',
    'photo',
    'profile',
    'qa',
    'main',
    'learn',
    'donate',
    'notifications',
    'articles',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.contenttypes',
    'grappelli',
    'django_extensions',
    'django.contrib.admin',
    'rest_framework',
    'djkombu',
    'autofixture',
    'crispy_forms'    
)

命令“runserser”,“makemigrations”或“migrate”工作正常,但createsuperuser返回此

Traceback (most recent call last):
  File "/opt/pycharm-3.0.2/helpers/pycharm/django_manage.py", line 23, in <module>
    run_module(manage_file, None, '__main__', True)
  File "/usr/lib/python2.7/runpy.py", line 176, in run_module
    fname, loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 82, in _run_module_code
    mod_name, mod_fname, mod_loader, pkg_name)
  File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/camaro/programming/imedrese/master/manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "/home/camaro/programming/imedrese/master/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/camaro/programming/imedrese/master/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/camaro/programming/imedrese/master/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 190, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/home/camaro/programming/imedrese/master/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 41, in load_command_class
    return module.Command()
  File "/home/camaro/programming/imedrese/master/local/lib/python2.7/site-packages/django/contrib/auth/management/commands/createsuperuser.py", line 28, in __init__
    self.UserModel = get_user_model()
  File "/home/camaro/programming/imedrese/master/local/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 'profile.Profile' that has not been installed

1 个答案:

答案 0 :(得分:0)

我的声誉不足以对您的问题发表评论,因此我将此作为答案,即使它没有回答您的问题。

根据扩展User模型的docs,您的方法似乎 - 抱歉 - 错误。就我从您的代码中看到的情况而言,除了向帐户添加更多信息之外,您对Profile没有做任何特别的事情。这可以使用OneToOne - 与原始Django用户的关系来完成(如链接文档中所述)。

说:我无法理解为什么你的模型在升级到1.8时应该停止工作。如果他们更改了有关自定义用户模型的内容,您是否检查了1.7 release notes1.8 release notes