views:

187

answers:

3

Hi,

I started learning Django recently and am having a strange problem with the tutorial. Everything was going fine until I started playing with the interactive shell and then I got an error whenever I tried to call all the objects in one of the tables.

I am using Django 1.1, Python 2.5 on MacOs X.

For those unfamiliar with the tutorial you are making a website to manage Polls. You have the following code in the model:

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

After creating the model you add a poll item and then add some choices to it. Everything was fine until I tried to see all the objects in the choices table or tried to see all the choices in a particular poll. Then I got an error. Heres an example series of commands in the interactive shell. Please note that the count of the choices is correct (I have experimented a bit after running into the error so the count is a bit high.)

>>> 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

The Django tutorial(part 1) can be found here

Thanks!

A: 

It looks like the problem is that was_published_today() is comparing a datetime to a date. Try changing it to:

return self.pub_date.date() == datetime.date.today()
Steve Losh
Yes my code was wrong but that didn't fix the problem. Thanks.
Juan Besa
A: 

Since the problem seems to be in the code that interprets strings as timestamps, I'd be interested to see the actual data in the db. It looks like there's a timestamp in there that isn't in the proper form. Not sure how it got there without seeing it, but I bet there's a clue there.

Ned Batchelder
Here you go:sqlite3 testDatabase "select * from polls_choice"1|1|Not much|02|1|The sky|03|1|just hacking again|04|1|just hacking again|05|1|Nothin|06|1|Stars|07|1|Stars|128|2|Not much|09|2|The sky|010|2|Just hacking again|0$ sqlite3 testDatabase "select * from polls_poll"1|What's up|2009-12-11 17:03:252|Yups|2009-12-11 14:22:55.891583
Juan Besa
A: 

The problem seemed to be that the database was not synchronized with the database. Resetting the database worked fine. Thanks to Alasdair for the suggestion.

Juan Besa