我最近开始学习Django,并在教程中遇到了一个奇怪的问题。一切都很顺利,直到我开始使用交互式shell,然后每当我尝试调用其中一个表中的所有对象时,都会出错。Django官方教程第1部分索引出界错误
我MacOS X上
对于那些不熟悉的教程使用Django 1.1,Python 2.5的你在做一个网站来管理投票。你必须在模型下面的代码:
from django.db import models
import datetime
class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __unicode__(self):
return self.question
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()
was_published_today.short_description = 'Published today?'
class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.choice
创建你添加一个调查项目,然后选择一些添加到它的模型之后。 一切都很好,直到我试图查看选择表中的所有对象或试图查看特定投票中的所有选项。然后我得到一个错误。下面是交互式shell中的一系列示例命令。请注意,选择的计数是正确的(我已经运行到错误,以便计数有点高后尝试了一下。)
>>> from mysite.polls.models import Poll, Choice
>>> Poll.objects.all()
[<Poll: What's up>, <Poll: Yups>]
>>> Choice.objects.count()
10
>>> Choice.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Library/Python/2.5/site-packages/django/db/models/query.py", line 68, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/Library/Python/2.5/site-packages/django/db/models/query.py", line 83, in __len__
self._result_cache.extend(list(self._iter))
File "/Library/Python/2.5/site-packages/django/db/models/query.py", line 238, in iterator
for row in self.query.results_iter():
File "/Library/Python/2.5/site-packages/django/db/models/sql/query.py", line 287, in results_iter
for rows in self.execute_sql(MULTI):
File "/Library/Python/2.5/site-packages/django/db/models/sql/query.py", line 2369, in execute_sql
cursor.execute(sql, params)
File "/Library/Python/2.5/site-packages/django/db/backends/util.py", line 19, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.5/site-packages/django/db/backends/sqlite3/base.py", line 193, in execute
return Database.Cursor.execute(self, query, params)
File "/Library/Python/2.5/site-packages/django/db/backends/util.py", line 82, in typecast_timestamp
seconds = times[2]
IndexError: list index out of range
Django的教程(第1部分)可以发现here
谢谢!
我假设你使用的是Django 1.1,Python 2.5和Mac OS X? – Boldewyn 2009-12-14 21:16:21
是将其添加到问题中。谢谢... – 2009-12-14 21:30:46
为什么你在short_description附加was_published_today方法? – czarchaic 2009-12-14 22:45:08