Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django p.choice_set.all()返回错误_Python_Mysql_Django_Django Models - Fatal编程技术网

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

Python Django p.choice_set.all()返回错误,python,mysql,django,django-models,Python,Mysql,Django,Django Models,我正在关注这一点。这是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 publi

我正在关注这一点。这是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\u 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")
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“C:\Python27\lib\site packages\django\db\models\query.py”,第71行,在__
数据=列表(自身[:报告输出大小+1])
文件“C:\Python27\lib\site packages\django\db\models\query.py”,第96行,在__
self._fetch_all()
文件“C:\Python27\lib\site packages\django\db\models\query.py”,第857行,在“fetch\u all”中
self.\u result\u cache=list(self.iterator())
迭代器中的第220行文件“C:\Python27\lib\site packages\django\db\models\query.py”
对于编译器.results\u iter()中的行:
文件“C:\Python27\lib\site packages\django\db\models\sql\compiler.py”,第713行,在results\u iter中
对于self.execute_sql(多)中的行:
文件“C:\Python27\lib\site packages\django\db\models\sql\compiler.py”,第786行,在execute\U sql中
cursor.execute(sql,params)
文件“C:\Python27\lib\site packages\django\db\backends\util.py”,第69行,执行
返回super(CursorDebugWrapper,self).execute(sql,params)
文件“C:\Python27\lib\site packages\django\db\backends\util.py”,第53行,执行
返回self.cursor.execute(sql,params)
文件“C:\Python27\lib\site packages\django\db\utils.py”,第99行,在退出时__
6.重新播放(dj_exc_类型、dj_exc_值、回溯)
文件“C:\Python27\lib\site packages\django\db\backends\util.py”,第53行,执行
返回self.cursor.execute(sql,params)
文件“C:\Python27\lib\site packages\django\db\backends\mysql\base.py”,执行中的第124行
返回self.cursor.execute(查询,参数)
文件“c:\users\Yax\appdata\local\temp\easy\u install-eeowbg\MySQL\u python-1.2.5-py2.7-win32.egg.tmp\MySQLdb\c
ursors.py”,执行中的第205行
errorhandler(self、exc、value)
defaulterrorhandler中的第36行文件“c:\users\Yax\appdata\local\temp\easy\u install-eeowbg\MySQL\u python-1.2.5-py2.7-win32.egg.tmp\MySQLdb\connections.py”
提高errorclass,errorvalue
操作错误:(1054,“Champ'myapp\u choice.poll\u id'Inconu dans字段列表”)

我怎么能把这个修好?我有
syncdb
my
models.py
,但仍然不工作。

syncdb
不会修改表或执行任何会破坏数据的操作。这意味着,如果添加列,则需要删除该表并使用syncdb重新启动

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

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

如果已安装数据库的客户端库,则可以通过管道将此命令的结果传送到:

python manage.py dbshell
显示您的模型定义。什么是
p
?@alecxe:问题已经编辑好了。谢谢。但是我使用的是MySQL。
python manage.py dbshell < python manage.py sqlclear myapp