Models Tutorial Django

4 views
Skip to first unread message

HelloWorld

unread,
May 11, 2010, 5:48:03 AM5/11/10
to Django users
Hi everybody

I have to excuse myself for the newbie question which is following
now:

Now I am doing the Poll/Models Tutorial on this page

http://docs.djangoproject.com/en/dev/intro/tutorial01/

and I am stuck at this point:

Wait a minute. <Poll: Poll object> is, utterly, an unhelpful
representation of this object. Let's fix that by editing the polls
model (in the polls/models.py file) and adding a __unicode__()
method to both Poll and Choice:

I changed the code in the models.py to this:

import datetime
from django.db import models

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



class Choice(models.Model):
poll = models.ForeignKey(Poll)
choice = models.CharField(max_length=200)
votes = models.IntegerField()
def __unicode__(self):
return self.question


I know the structure is wrong.
Any suggestions would be greatly appreciated.

Thanks!!

Best

Z

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Jirka Vejrazka

unread,
May 11, 2010, 6:03:57 AM5/11/10
to django...@googlegroups.com
> class Choice(models.Model):
>    poll = models.ForeignKey(Poll)
>    choice = models.CharField(max_length=200)
>    votes = models.IntegerField()
>    def __unicode__(self):
>        return self.question
>
>
> I know the structure is wrong.
> Any suggestions would be greatly appreciated.

Hi,

I'm not sure why you'd think the structure is wrong (i.e. state
actual errors or problems rather than generic statements).

However there is one problem with your Choice model. It does not
have any self.question, so __unicode__() can't really return it. You
probaby want to use "return self.choice" there (or compose some text
string based on existing model fields).

HTH

Jirka

HelloWorld

unread,
May 11, 2010, 6:15:28 AM5/11/10
to Django users
Hi Jirka

Thanks for your answer!

By structure I mean, I just followed the tutorial and am not sure if
the position and order of these tutorial code is right in my code:

class Poll(models.Model):
# ...
def __unicode__(self):
return self.question

class Choice(models.Model):
# ...
def __unicode__(self):
return self.choice


AND

import datetime
# ...
class Poll(models.Model):
# ...
def was_published_today(self):
return self.pub_date.date() == datetime.date.today()


And I know the result is wrong because in the API it does not return
what the tutorial says should be returned:

TUTORIAL:

>>> from mysite.polls.models import Poll, Choice

# Make sure our __unicode__() addition worked.
>>> Poll.objects.all()
[<Poll: What's up?>]


ME:

>>> Poll.objects.all()
[<Poll: Poll object>]

Thanks for the time!

Best

Z.

Jirka Vejrazka

unread,
May 11, 2010, 7:38:52 AM5/11/10
to django...@googlegroups.com
Hmm, I can't see anything wrong with your code, so I'm gonna go to the
basics - how about your code indentation? Is the "def __unicode__()"
the same way as your model field definitions?
The __unicode__() function must be part of your Poll class, not standalone.

Cheers

Jirka

Daniel Roseman

unread,
May 11, 2010, 9:14:17 AM5/11/10
to Django users
A simple suggestion: have you quit and reloaded the shell?
--
DR.

Karen Tracey

unread,
May 11, 2010, 10:18:16 AM5/11/10
to django...@googlegroups.com
On Tue, May 11, 2010 at 6:15 AM, HelloWorld <zucke...@gmail.com> wrote:
Hi Jirka

Thanks for your answer!

By structure I mean, I just followed the tutorial and am not sure if
the position and order of these tutorial code is right in my code:

class Poll(models.Model):
   # ...
   def __unicode__(self):
       return self.question

class Choice(models.Model):
   # ...
   def __unicode__(self):
       return self.choice


AND

import datetime
# ...
class Poll(models.Model):
   # ...
   def was_published_today(self):
       return self.pub_date.date() == datetime.date.today()


If you are saying that you repeated the "class Poll" line, first with the __unicode__ method defined and then later with the was_published_today method, you are right that that is incorrect. The "class Poll(model.Model):" line should appear only once in the file, and indented beneath it should be all of the fields and methods for the Poll model. If you repeat "class Poll...", then whatever follows that line will completely replace the previous definition for the Poll class, which is not what you want, and would lead to the result you show (no apparent definition of the __unicode__ method for Poll, since you overwrite the Poll-with-unicode version with the Poll-with-was_published_today version).

Karen
--
http://tracey.org/kmt/
Reply all
Reply to author
Forward
0 new messages