我正在努力弄清楚为什么我的自定义用户配置文件 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/