2009-12-14 73 views
3

我最近开始学习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

谢谢!

+0

我假设你使用的是Django 1.1,Python 2.5和Mac OS X? – Boldewyn 2009-12-14 21:16:21

+0

是将其添加到问题中。谢谢... – 2009-12-14 21:30:46

+0

为什么你在short_description附加was_published_today方法? – czarchaic 2009-12-14 22:45:08

回答

1

问题似乎是数据库未与模型同步。重置数据库工作正常。感谢Alasdair的建议。

0

看起来问题是was_published_today()正在比较日期时间和日期。尝试将其更改为:

return self.pub_date.date() == datetime.date.today() 
+0

是的,我的代码错了,但没有解决问题。谢谢。 – 2009-12-14 21:31:39

0

因为问题似乎是在解释字符串作为时间戳的代码,我很想看到在数据库中的实际数据。它看起来像那里有一个时间戳,它不是正确的形式。不知道它怎么没有看到它,但我敢打赌,那里有线索。

+0

在这里你去: sqlite3的testDatabase “从polls_choice选择*” 1 | 1 |不多| 0 2 | 1 |天空| 0 3 | 1 |只是再次黑客| 0 4 | 1 |只是再次黑客| 0 5 | 1 |没什么| 0 6 | 1 |星| 0 7 | 1 |星| 12 8 | 2 |不多| 0 9 | 2 |天空| 0 10 | 2 |只要再次黑客| 0 $ sqlite3 testDatabase“select * from polls_poll” 1 | What's up | 2009-12-11 17:03:25 2 | Yups | 2009-12-11 14:22:55.891583 – 2009-12-15 15:55:16