Tutorial

21 views
Skip to first unread message

jk121960

unread,
Apr 6, 2012, 2:13:39 PM4/6/12
to Django users
Hi, I am going through the tutorial, I am experienced programmer and I
am learning django. Every thing was fine till the portion where you
add the __unicode() methods to the classes. When I execute the
Poll.objects.all() ant the command line it doesn't look any
different."[<Poll: Poll object>]" just like the first python console
example. I tried to run syncdb thinking this was what was missing but
it didn't help. It stated that it
"Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)"

like before but no change to the output in python. Can someone suggest
something so that I can move on from this part, I worked with this
some time back but was unable to utilize it. I am attempting to change
that now, ao I was going back through the lessons.


thanks and any help is appreciated

--jerry

Jonathan Baker

unread,
Apr 6, 2012, 2:54:47 PM4/6/12
to django...@googlegroups.com
Jerry,

You'll want to be sure your method is defined as:

def __unicode__(self):
    return self.name # I'm using 'name' as an example field

with double underscores on both sides of 'unicode' instead of just in front of it.

Hope this helps,
Jonathan


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




--
Jonathan D. Baker
Web Developer
http://jonathandbaker.com
303.257.4144

Gerald Klein

unread,
Apr 6, 2012, 5:32:38 PM4/6/12
to django...@googlegroups.com
Hi and thanks for the response here is the code minus the line numbers -- copied from vim, all my tabs are right and I had the right underscores. 


Please let me know if you see something. 

thanks again

--jerry

 class Poll(models.Model):
  8     question = models.CharField(max_length=200)
  9     pub_date = models.DateTimeField('date published')
 10 
 11 class Choice(models.Model):
 12     poll = models.ForeignKey(Poll)
 13     choice = models.CharField(max_length=200)
 14     votes = models.IntegerField()
 15 
 16 class Poll(models.Model):
 17     def __unicode__(self):
 18         return self.question
 19     def was_published_recently(self):
 20         return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
 21 
 22 class Choice(models.Model):
 23     def __unicode__(self):
 24         return self.choice

Gerald Klein DBA

Cont...@geraldklein.com

www.geraldklein.com

j...@zognet.com

708-599-0352


Linux registered user #548580 



Jonathan Baker

unread,
Apr 6, 2012, 5:42:13 PM4/6/12
to django...@googlegroups.com
You redeclaring your Model classes, and you don't need to. By doing so, the 'self' inside your __unicode__ function refers to a property that isn't defined (since you redeclared the original class that had the property). Here is what the model should look like: http://codepad.org/P21ZNkZY

Gerald Klein

unread,
Apr 6, 2012, 7:58:02 PM4/6/12
to django...@googlegroups.com
OK makes a complete sense and worked the first time thanks very much.

--jerry

Jonathan Baker

unread,
Apr 6, 2012, 8:24:43 PM4/6/12
to django...@googlegroups.com
Glad to help!
Reply all
Reply to author
Forward
0 new messages