Django 'No such table:"用于自定义用户配置文件,为什么?

标签 django django-views django-allauth

我正在努力弄清楚为什么我的自定义用户配置文件 View 不再有效。我正在使用 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)

我的 View 是一个设置用户配置文件设置的地方,如下所示:
@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 migrate 创建它。和 python manage.py syncdb .

最佳答案

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

你提到你一直在运行迁移,但实际迁移已经存在了吗?

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

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

如果仍然没有,你当然可以从头开始。我可能错过了某个地方的捷径,但我会:

  • 创建您需要保留的数据副本
    ( python manage.py dumpdata --indent=4 <appname> )(将其保存到装置中)
  • 删除迁移文件
  • 删除 db.sqlite3
  • 运行 python manage.py makemigrations
  • 运行 python manage.py migrate
  • 使用 python manage.py loaddata <fixture-name>重新加载旧数据
  • 关于Django 'No such table:"用于自定义用户配置文件,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28385919/

    相关文章:

    django - Jinja2 django 1.6 如何消除包含标签中的引号?

    django - SessionWizardView 状态仅保存在最终表单上,done() 未执行

    python - 使用 django_facebook 从 Facebook 获取用户信息

    django-reversion 及相关模型

    python - django-rest-framework : restrict RelatedField queryset according to request

    python - Django无法处理html表单数据

    python - 使用关闭按钮在 DJango 中显示 flash 消息

    需要 Django all-auth 电子邮件

    django - 我应该在 'Server IP Whitelist' facebook 开发者控制台上放置哪个 IP 地址? (来自 Facebook 的 400 错误)

    javascript - Django 不能很好地使用简单的 angularjs,不知道为什么