首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django p.choice_set.all()返回错误

Django p.choice_set.all()返回错误
EN

Stack Overflow用户
提问于 2014-07-18 17:38:10
回答 1查看 253关注 0票数 1

我正在跟踪这个教程。是Django 1.6。

代码语言:javascript
复制
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()会返回以下错误:

代码语言:javascript
复制
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,但仍然不工作。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-18 18:57:52

syncdb不会修改表,也不会做任何破坏数据的事情。这意味着,如果添加列,则需要删除表,然后用syncdb重新开始。

错误是指出您丢失了一个字段。因此,您应该删除sqlite数据库并再次运行syncdb。

最简单的方法是使用管理命令,它将打印出为应用程序重置数据库所需执行的SQL。

如果安装了数据库的客户端库,则可以将此命令的结果传递到管理命令

代码语言:javascript
复制
python manage.py dbshell < python manage.py sqlclear myapp
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24830819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档