def__str__(self) not working properly

1,881 views
Skip to first unread message

Gary Roach

unread,
Jul 1, 2016, 5:52:12 AM7/1/16
to django-users
Hi all;

I am working on the official django tutorial
(https//docs.djangoproject.com/en/1.9/intro/tutorial/02/) on the section
that is adding the choices. Specifically:


# Create three choices.
>>> q.choice_set.create(choice_text='Not much', votes=0)
<Choice: Not much>
>>> q.choice_set.create(choice_text='The sky', votes=0)
<Choice: The sky>
>>> c = q.choice_set.create(choice_text='Just hacking again', votes=0)

The "Not much" entry should return <Choice: Not much> but returns
<Choice: Choice object> instead.

This would seem to be a problem with the models.py def__str__(self)
call, but I can't locate the problem. Question class works fine. The
specific code is :

import datetime

from django.db import models
from django.utils import timezone


class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')

def __str__(self):
return self.question_text

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


class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)

def __str__(self):
return self.choice_text

OS Debian Stretch
KDE Desktop
Django 1.9
python 3.5
Eclipse PyDev IDE

All migrations are up to date.

Any help will be sincerely appreciated.

Gary R.

Joey Chang

unread,
Jul 1, 2016, 7:33:34 AM7/1/16
to django...@googlegroups.com



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3219730a-2d8b-ee3b-3374-72c64ccd3a8e%40verizon.net.
For more options, visit https://groups.google.com/d/optout.

Tim Graham

unread,
Jul 1, 2016, 7:32:53 PM7/1/16
to Django users
Hm, not much can go wrong there. Just to be sure, did you start the shell after you added the __str__ method?

Charlie c

unread,
Jul 3, 2016, 10:20:40 AM7/3/16
to Django users

The line is indented 4 spaces yes?

Gary Roach

unread,
Jul 3, 2016, 4:55:17 PM7/3/16
to django...@googlegroups.com
Yes it is

Gary R
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Daniel Herrera

unread,
Sep 16, 2018, 7:44:18 AM9/16/18
to Django users
I was having the same issue, as mentioned above, it was a mere indentation problem, make sure the def __str__ is properly indented inside of the class and not at the same level.

Aginjith G J

unread,
Aug 25, 2020, 4:57:46 PM8/25/20
to Django users
0

Do this.

def __repr__ (self): return self.title

restart the shell/session.

then check.

i had this issue. only after restarting the section it worked 

Reply all
Reply to author
Forward
0 new messages