I'm just going through the official tutorial for Django: "Writing your first Django app, part 1"
At the bottom of the page it says:
# Make sure our custom method worked.
>>> p = Poll.objects.get(pk=1)
>>> p.was_published_recently()
True
So far everything went well but now cmd returns:
TypeError: unsupported operand type(s) for -: 'function' and
'datetime.timedelta'
WHAT CAN I DO TO FIX IT??
So far my cone in \polls\models.py:
from django.db import models
import datetime
from django.utils import timezone
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_recently(self):
return self.pub_date >= timezone.now - datetime.timedelta(days=1)
class Choice(models.Model):
poll= models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.choice
def was_published_recently(self):
return self.pub_date >= timezone.now - datetime.timedelta(days=1)
>>> from django.utils import timezone >>> p = Poll(question="What's new?", pub_date=timezone.now()) # Save the object into the database. You have to call save() explicitly. >>> p.save() # Now it has an ID. Note that this might say "1L" instead of "1", depending # on which database you're using. That's no biggie; it just means your # database backend prefers to return integers as Python long integer # objects. >>> p.id 1 <----- I HAVE 3
Poll.objects.filter(question__startswith='What')
[<Poll: What's up?>]
p = Poll.objects.get(pk=1)
p.choice_set.all()
[]