unsupported opperand type(s)

78 views
Skip to first unread message

Mariusz Wilk

unread,
Aug 1, 2014, 4:09:27 PM8/1/14
to django...@googlegroups.com
Hi guys.

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

Collin Anderson

unread,
Aug 1, 2014, 4:32:32 PM8/1/14
to django...@googlegroups.com

    def was_published_recently(self):
        return self.pub_date >= timezone.now - datetime.timedelta(days=1)

timezone.now() 

Mariusz Wilk

unread,
Aug 1, 2014, 5:31:39 PM8/1/14
to django...@googlegroups.com
thanks, that helped

I also got confused and created too many objects, so now instead of  p.id  "1" cdm returns  "3". How do I delete objects from p?
>>> 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 

Collin Anderson

unread,
Aug 1, 2014, 6:05:28 PM8/1/14
to django...@googlegroups.com
p.id == 3 is just fine. It's auto-incrementing, and even if you delete items, an object will never get p.id == 1 again.

If you really want a Poll with id=1, either clear out your database, or create it manually like so:
p = Poll(id=1, question="What's new?", pub_date=timezone.now())
p
.save()

Mariusz Wilk

unread,
Aug 2, 2014, 5:52:09 AM8/2/14
to django...@googlegroups.com
But when I enter:

= Poll(id=1, question="What's new?", pub_date=timezone.now())
p
.save()

and then enter:
Poll.objects.filter(question__startswith='What')
it should return:
[<Poll: What's up?>]
but it returns:

[<Poll: Poll object>, <Poll: Poll object>, <Poll: Poll object>]


..and so other commands form the tutorial also return things completely different from what they sopposed to.

I also tried deleting Polls file from mysite altogether, but when I got to  Poll.objects.all() command it still returned 3 objects instead of one. At this point I just wish there was a way to start everything from the top without uninstalling Django.



Collin Anderson

unread,
Aug 2, 2014, 10:33:33 AM8/2/14
to django...@googlegroups.com
What version of python are you using (2 or 3)?

Poll.objects.all().delete() will delete all of your Poll objects, if you want. Or it may be simpler to just rm db.sqlite3 and run manage.py migrate/syncdb again.

Mariusz Wilk

unread,
Aug 4, 2014, 6:33:17 AM8/4/14
to django...@googlegroups.com
Kinda worked. I deleted my Poll objects and created a new one. But then, a few more lines into the tutorial, there are these commands one after another:
p = Poll.objects.get(pk=1)
p.choice_set.all()
I get: OperationalError: no such column: polls_choice.choice_text

It should be simply:
[]
P.S. I'm using Python 3.4.1

Collin Anderson

unread,
Aug 4, 2014, 8:34:41 AM8/4/14
to django...@googlegroups.com
What version of django are you using?

Mariusz Wilk

unread,
Aug 4, 2014, 1:02:25 PM8/4/14
to django...@googlegroups.com
Django-1.6.5

Collin Anderson

unread,
Aug 4, 2014, 1:11:14 PM8/4/14
to django...@googlegroups.com
If you have no data that is important, I'd just delete the database and start fresh. If you're using sqlite you can simply `rm db.sqlite3` and then run python manage.py migrate again.
Reply all
Reply to author
Forward
0 new messages