gpt4 book ai didi

python - Django p.choice_set.all() 返回错误

转载 作者:行者123 更新时间:2023-11-29 00:11:35 24 4
gpt4 key购买 nike

我正在关注这个 tutorial .它是 Django 1.6。

from django.db import models
import datetime
from django.utils import timezone
# Create your models here.

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self): # Python 3: def __str__(self):
return self.question
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

def __unicode__(self): # Python 3: def __str__(self):
return self.choice_text

我有 p = Poll.objects.get(pk=1) 并返回我插入的数据但是运行 p.choice_set.all() 返回错误如下:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 71, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 96, in __iter__
self._fetch_all()
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 857, in _fetch_all
self._result_cache = list(self.iterator())
File "C:\Python27\lib\site-packages\django\db\models\query.py", line 220, in iterator
for row in compiler.results_iter():
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 713, in results_iter
for rows in self.execute_sql(MULTI):
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 69, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py", line 99, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\util.py", line 53, in execute
return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 124, in execute
return self.cursor.execute(query, args)
File "c:\users\Yax\appdata\local\temp\easy_install-eeowbg\MySQL_python-1.2.5-py2.7-win32.egg.tmp\MySQLdb\c
ursors.py", line 205, in execute
self.errorhandler(self, exc, value)
File "c:\users\Yax\appdata\local\temp\easy_install-eeowbg\MySQL_python-1.2.5-py2.7-win32.egg.tmp\MySQLdb\connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
OperationalError: (1054, "Champ 'myapp_choice.poll_id' inconnu dans field list")

我怎样才能解决这个问题?我有 syncdb 我的 models.py 但仍然无法正常工作。

最佳答案

syncdb 不会修改表或做任何会破坏数据的事情。这意味着如果你添加一个列,你需要删除表并重新开始使用 syncdb。

错误表明您缺少一个字段。所以你应该删除 sqlite 数据库并再次运行 syncdb。

最简单的方法是使用 sqlclear management command ,它将打印出您需要执行的 SQL 以重置应用程序的数据库。

如果您安装了数据库的客户端库,您可以将此命令的结果通过管道传递给 dbshell management command :

python manage.py dbshell < python manage.py sqlclear myapp

关于python - Django p.choice_set.all() 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24830819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com