The Official Django Guide Part 1: Index from a Related Error

I recently started learning Django, and I had a strange problem with a tutorial. Everything went fine until I started playing with the interactive shell, and then I received an error message when I tried to call all objects in one of the tables.

I am using Django 1.1, Python 2.5 on Mac OS X.

For those unfamiliar with the tutorial, you are creating a survey management website. The model has the following code:

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

, . , . . . , ( , .)

>>> from mysite.polls.models import Poll, Choice
>>> Poll.objects.all()
[<Poll: What 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)

!

+3
3

, . . .

+1

, , was_published_today() . :

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

Since the problem seems to be in code that interprets strings as timestamps, I would be interested to see the actual data in db. It looks like there is a timestamp that is not in the correct form. Not sure how he got there without seeing this, but I bet that there is a key.

0
source

Source: https://habr.com/ru/post/1725658/


All Articles