Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » tutorial __str__() method not working [RSS Feed]

#1 March 26, 2008 17:59:35

[EMAIL P.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

tutorial __str__() method not working


Hello,

I'm just going through the mysite tutorial and can't seem to get the
__str__() method to work when adding it to the two classes, I still
get .

polls/models.py:

from django.db import models
import datetime

class Poll(models.Model):
question = models.CharField(maxlength=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question
def was_published_today(self):
return self.pubdate.date() == datetime.date.today()

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

Thanks for any help,

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

Offline

#2 March 26, 2008 18:12:29

Tim S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

tutorial __str__() method not working


Looks like your indentation is wrong (assuming it's not just a copy/paste
issue).

Remember python code needs to be indented correctly in order to work. Your
def __str__(self): line needs to start at the same indentation as the
attributes (question/pub_date) inside your class.

Tim.

On Wednesday 26 Mar 2008, wrote:
> Hello,
>
> I'm just going through the mysite tutorial and can't seem to get the
> __str__() method to work when adding it to the two classes, I still
> get .
>
> polls/models.py:
>
> from django.db import models
> import datetime
>
> class Poll(models.Model):
> question = models.CharField(maxlength=200)
> pub_date = models.DateTimeField('date published')
> def __str__(self):
> return self.question
> def was_published_today(self):
> return self.pubdate.date() == datetime.date.today()
>
> class Choice(models.Model):
> poll = models.ForeignKey(Poll)
> choice = models.CharField(maxlength=200)
> votes = models.IntegerField()
> def __str__(self):
> return self.choice
>
> Thanks for any help,
>
> Jason
>


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

Offline

#3 March 26, 2008 18:21:54

Evert R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

tutorial __str__() method not working


> I'm just going through the mysite tutorial and can't seem to get the
> __str__() method to work when adding it to the two classes, I still
> get .
>
> polls/models.py:
>
> from django.db import models
> import datetime
>
> class Poll(models.Model):
> question = models.CharField(maxlength=200)
> pub_date = models.DateTimeField('date published')
> def __str__(self):
> return self.question
> def was_published_today(self):
> return self.pubdate.date() == datetime.date.today()
>
> class Choice(models.Model):
> poll = models.ForeignKey(Poll)
> choice = models.CharField(maxlength=200)
> votes = models.IntegerField()
> def __str__(self):
> return self.choice

Are you using django 0.96, or the SVN version? For the latter, you'll
need to change __str__() to __unicode__():http://www.djangoproject.com/documentation/tutorial01/and search for __str__


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

Offline

#4 March 26, 2008 18:57:02

Erik V.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

tutorial __str__() method not working


It looks like the issue is one of indentation. There's nothing for
"self" to refer to if the method isn't part of the class.

--And you're probably better off using __unicode__ than __str__

Try:

class Poll(models.Model):
question = models.CharField(max_length=200)
pub_date = models.DateField('date published')

def __unicode__(self):
return self.question

On Wed, Mar 26, 2008 at 11:59 AM, <> wrote:
>
> Hello,
>
> I'm just going through the mysite tutorial and can't seem to get the
> __str__() method to work when adding it to the two classes, I still
> get .
>
> polls/models.py:
>
> from django.db import models
> import datetime
>
> class Poll(models.Model):
> question = models.CharField(maxlength=200)
> pub_date = models.DateTimeField('date published')
> def __str__(self):
> return self.question
> def was_published_today(self):
> return self.pubdate.date() == datetime.date.today()
>
> class Choice(models.Model):
> poll = models.ForeignKey(Poll)
> choice = models.CharField(maxlength=200)
> votes = models.IntegerField()
> def __str__(self):
> return self.choice
>
> Thanks for any help,
>
> Jason
> >
>

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

Offline

  • Root
  • » Django
  • » tutorial __str__() method not working [RSS Feed]

Board footer

Moderator control

Enjoy the 16th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support