Django'没有这样的表:“对于自定义用户配置文件,为什么?

时间:2015-02-07 18:41:04

标签: django django-views django-allauth

我试图弄清楚我的自定义用户个人资料视图不再有效的原因。我使用django-allauth,并通过为用户创建具有一对一字段的模型来自定义我的用户配置文件。但是,我的观点是抛出错误:

OperationalError at /profile/

no such table: user_profile

Request Method:     GET
Request URL:    http://localhost:8000/profile/
Django Version:     1.7.4
Exception Type:     OperationalError
Exception Value:    

no such table: user_profile

Exception Location:     /usr/local/lib/python2.7/dist-packages/django/db/backends/sqlite3/base.py in execute, line 485
Python Executable:  /usr/bin/python
Python Version:     2.7.6

我的models.py以及自定义用户个人资料如下所示:

from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.db import models
from allauth.account.models import EmailAddress


class UserProfile(models.Model):
    user = models.OneToOneField(User, related_name='profile')

    # user visible settings
    stop_reminders = models.BooleanField ( default=False,
                             help_text = 'Flag if user wants reminders not to be sent.' )

    stop_all_email = models.BooleanField ( default=False,
                             help_text = 'Flag if user wants no email to be sent.' )

    # hidden settings
    is_premium =  models.BooleanField ( default=False,
                             help_text = 'Flag if user has the premium service.' )

    def __unicode__(self):
        return "{}'s profile".format(self.user.username)

    class Meta:
        db_table = 'user_profile'

    def account_verified(self):
        if self.user.is_authenticated:
            result = EmailAddress.objects.filter(email=self.user.email)
            if len(result):
                return result[0].verified
        return False

#User.profile = property(lambda u: UserProfile.objects.get_or_create(user=u)[0])

def create_user_profile(sender, instance, created, **kwargs):
    if created:
       profile, created = UserProfile.objects.get_or_create(user=instance)

post_save.connect(create_user_profile, sender=User)

我的观点是设置用户个人资料设置的地方,如下所示:

@login_required
def user_settings (request):
    """
    User settings view, handles changing of user settings and
    entry distribution (eventually)
    """

    if request.method == 'POST': # If the form has been submitted...

        form = UserProfileForm(request.POST, instance=request.user.profile) # A form bound to the POST data

        if form.is_valid(): # All validation rules pass
            form.save ()

            return render_to_response('sm/profile_update_success.html',
                                      {},
                                      context_instance=RequestContext(request))

    else:
        # create unbound form with initial values generated
        # from the user's profile
        form = UserProfileForm (instance=request.user.profile)

    return render ( request, 'profile.html', { 'form': form, } )

直到最近,这个工作正常,而且我不确定是什么改变了让它停止。有人可以解释一下吗?

我使用的是Django-1.7.4和django-allauth-0.19.1。对于调试,我使用sqllite,我尝试使用python manage.py migratepython manage.py syncdb进行创建。

4 个答案:

答案 0 :(得分:2)

首先,如果之前有效,我无法解释为什么它不再存在。 你应该检查sqlite文件本身,看看表是否存在。 (该文件通常位于根目录db.sqlite3,可以使用任何标准的sqlite浏览器打开)

您提到您一直在运行迁移,但实际迁移是否已经存在?

如果您还没有,请先运行python manage.py makemigrations。如果需要,请验证迁移文件本身。

还有python manage.py inspectdb命令可以让您了解您正在使用的模型。

如果仍然没有,你当然可以从头开始。我可能错过了某处的捷径,但我还是:

  1. 创建您需要保留的数据副本 (python manage.py dumpdata --indent=4 <appname>)(将此保存为固定装置)
  2. 删除迁移文件
  3. 删除db.sqlite3
  4. 运行python manage.py makemigrations
  5. 运行python manage.py migrate
  6. 使用python manage.py loaddata <fixture-name>重新加载旧数据

答案 1 :(得分:1)

Gahh,答案是我已经从INSTALLED_APPS评论了我的主应用程序的条目。这很愚蠢。

答案 2 :(得分:1)

我有同样的问题。为了解决这个问题,我通过Control + C退出了服务器。比通过运行以下两个命令进行迁移:$ python manage.py makemigrations 它告诉我 “个人资料”的迁移:

profiles / migrations / 0001_initial.py

- Create model profile

比我运行$ python manage.py migrate

它告诉我

  

要执行的操作:应用所有迁移:admin,auth,   内容类型,配置文件,会话正在运行迁移:

     

正在应用配置文件。0001_initial...确定

现在我再次运行服务器$ python manage.py runserver

,当我创建配置文件时,它会显示

  

[2018年8月24日19:36:16]“ GET / admin / profiles / profile / add / HTTP / 1.1” 200   4381

     

[2018年8月24日19:36:19]“ POST / admin / profiles / profile / add / HTTP / 1.1”   302 0

     

[2018年8月24日19:36:19]“ GET / admin / profiles / profile / HTTP / 1.1” 200   4376

     

[2018年8月24日19:36:19]“ GET / admin / jsi18n / HTTP / 1.1” 200 3217

     

[2018年8月24日19:36:19]“ GET /static/admin/css/changelists.css   HTTP / 1.1“ 200 6170

     

[2018年8月24日19:36:19]“ GET /static/admin/img/tooltag-add.svg   HTTP / 1.1“ 200 331

     

[2018年8月24日19:36:19]“ GET /static/admin/img/icon-yes.svg HTTP / 1.1”   200436

并创建了配置文件。希望有帮助。

答案 3 :(得分:0)

我有同样的问题,但对我来说解决方案是不同的。我在清理时意外删除了迁移目录中的 init .py,这意味着某些迁移未运行产生相同的错误。更换它解决了这个问题。