__unicode__() addition not working in basic poll application.

1,440 views
Skip to first unread message

maaz muqri

unread,
May 16, 2011, 8:32:41 AM5/16/11
to Django users
Hi,
____________________________________
class Poll(models.Model):
# ...
def __unicode__(self):
return self.question

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


after adding the above code also I am not able to retrieve the
question by the command:

I am getting this

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


instead of this

>>>Poll.objects.all()
[<Poll: What's up?>]

AJ

unread,
May 16, 2011, 9:58:52 AM5/16/11
to django...@googlegroups.com
I think you are getting the poll object back. You need to specify the method for your model class(es)


def __unicode__(self):
        return "%s %s %s" % (self.poll_name)


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




--
AJ

momo2k

unread,
May 16, 2011, 10:32:15 AM5/16/11
to Django users
Did you restart the interpreter after adding the code?

Gabe

unread,
May 16, 2011, 11:08:24 AM5/16/11
to Django users
Can it be necessary to recreate a model?

Fabian Ezequiel Gallina

unread,
May 16, 2011, 11:45:18 AM5/16/11
to django...@googlegroups.com
2011/5/16 AJ <brand...@gmail.com>:

> I think you are getting the poll object back. You need to specify the method
> for your model class(es)
>
> def __unicode__(self):
>
>         return "%s %s %s" % (self.poll_name)
>
>

That is wrong, you have a placeholder for three args and just pass one
to the string formatting. Moreover, you don't even need that here.

def __unicode__(self):
return self.poll_name

Probably a fast-reply kind of error, but that could lead the original
poster to even get more issues than helping him to solve it.

Regards,
--
Fabián E. Gallina
http://www.anue.biz

AJ

unread,
May 16, 2011, 11:53:45 AM5/16/11
to django...@googlegroups.com
Sorry about that. It is indeed not right. I will take care of this from next time onwards.


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




--
AJ

maaz muqri

unread,
May 17, 2011, 3:34:12 AM5/17/11
to Django users
but still I am getting same problem

Roman Klesel

unread,
May 17, 2011, 4:14:17 AM5/17/11
to django...@googlegroups.com
2011/5/16 maaz muqri <mmu...@mshahtech.com>:

> I am getting this
>
>>>>Poll.objects.all()
> [<Poll: Poll object>]

What do you get when you do:

print unicode(Poll.objects.all()[0])

?

Regards
Roman

maaz muqri

unread,
May 17, 2011, 6:59:42 AM5/17/11
to Django users
im just getting "Poll object" as output

Roman Klesel

unread,
May 17, 2011, 8:37:23 AM5/17/11
to django...@googlegroups.com
hmm... strange...

the __unicode__ thing is nothing django-specific. It works in plain
python as well:

It's supposed to work like this:

>>> class TestClass(object):
... def __unicode__(self):
... return "unicode"
... def __repr__(self):
... return "buh"
... def __str__(self):
... return "str"
...
>>> o=TestClass()
>>> o
buh
>>> print o
str
>>> print unicode(o)
unicode
>>>


2011/5/17 maaz muqri <mmu...@mshahtech.com>:

EPS

unread,
Jun 5, 2011, 1:20:21 PM6/5/11
to Django users
Hi, i found this post when try to solve same problem: my trouble was a
"spaces" in models.py.
in your first message it seems to be ok, but you must really check
spaces in your class describe.
http://mail.python.org/pipermail/tutor/2007-January/051903.html

Kyle Latham

unread,
Jun 14, 2011, 2:01:54 PM6/14/11
to Django users
Hello,

I am having the same problem while working through the tutorial. have
searched these forums and tried everything that was recommended. My
spacing is good also - I checked that. I am still getting the same
output:

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

Any ideas on how to fix my issue?

On Jun 5, 11:20 am, EPS <eremina....@gmail.com> wrote:
> Hi, i found this post when try to solve same problem: my trouble was a
> "spaces" in models.py.
> in your first message it seems to be ok, but you must really check
> spaces in your class describe.http://mail.python.org/pipermail/tutor/2007-January/051903.html
>
> On May 17, 4:59 pm, maaz muqri <mmu...@mshahtech.com> wrote:
>
>
>
> > im just getting "Pollobject" as output
>
> > On May 17, 1:14 pm, Roman Klesel <roman.kle...@googlemail.com> wrote:> 2011/5/16 maaz muqri <mmu...@mshahtech.com>:
>
> > > > I am getting this
>
> > > >>>>Poll.objects.all()
> > > > [<Poll:Pollobject>]
>
> > > What do you get when you do:
>
> > > print unicode(Poll.objects.all()[0])
>
> > > ?
>
> > > Regards
> > >  Roman- Hide quoted text -
>
> - Show quoted text -

Micky Hulse

unread,
Jun 14, 2011, 2:55:10 PM6/14/11
to django...@googlegroups.com
On Tue, Jun 14, 2011 at 11:01 AM, Kyle Latham <kly.l...@gmail.com> wrote:
>>>> Poll.objects.all()
> [<Poll: Poll object>]

What do you get when you try:

>>> p = Poll.objects.all()[0]
>>> p
>>> type(p)
>>> dir(p)

Try this too:

>>> p = Poll.objects.all()
>>> for x in p:
. . . print x (indent this line four spaces, hit return key once)
. . . (hit return key again)

Does any of that help?

Good luck!

Cheers,
Micky

Spartan

unread,
Jul 19, 2011, 10:09:21 PM7/19/11
to Django users
Hello,

I just started using django two days ago. I tried your method and

>>> p=Poll.objects.all()[0]
>>> p
#results in
<Poll: Poll object>
>>> type(p)
#results in
<class 'polls.models.Poll'>
>>> dir(p)
#gives an error
['DoesNotExist', 'MultipleObjectsReturned', '__class__', '__delattr__'
'__dict__', '__doc__', '__eq__', '__format__', '__getattribute__',
'_hash__', '__init__', '__metaclass__', '__module__', '__ne__',
'__new__, '__reduce__', '__reduce_ex__', '__repr__', '__setattr__',
'__sizeof_', '__str__', '__subclasshook__', '__weakref__',
'_base_manager', '_deault_manager', '_deferred', '_get_FIELD_display',
'_get_next_or_previos_by_FIELD', '_get_next_or_previous_in_order',
'_get_pk_val', '_get_unque_checks', '_meta', '_perform_date_checks',
'_perform_unique_checks' '_set_pk_val', '_state', '_unicode_',
'choice_set', 'clean', 'clean_felds', 'date_error_message', 'delete',
'full_clean', 'get_next_by_pub_ate', 'get_previous_by_pub_date', 'id',
'objects', 'pk', 'prepare_dataase_save', 'pub_date', 'question',
'save', 'save_base', 'serializable_alue', 'unique_error_message',
'validate_unique']
---------------------

>>> p =Poll.objects.all()
>>> for x in p:
. . . print x (indent this line four spaces, hit return key once)
. . . (hit return key again)
#results in
Poll object

-----------

help 0_0


On Jun 14, 9:55 pm, Micky Hulse <rgmi...@gmail.com> wrote:
> On Tue, Jun 14, 2011 at 11:01 AM, Kyle Latham <kly.lat...@gmail.com> wrote:
> >>>>Poll.objects.all()
> > [<Poll:Pollobject>]
>
> What do you get when you try:
>
> >>> p =Poll.objects.all()[0]

Tom Evans

unread,
Nov 13, 2012, 11:09:59 AM11/13/12
to django...@googlegroups.com
On Tue, Nov 13, 2012 at 8:35 AM, Colin Keenan <colinn...@gmail.com> wrote:
> I found out the reason `__str__()` worked for me but `__unicode__()` didn't.
> It's because python3 already uses unicode in the `__str__` method, so if
> using python3, don't use `__unicode__`.

You should be fore-warned that Django does not officially support Python 3.

https://docs.djangoproject.com/en/1.4/releases/1.4/#python-compatibility

If you are using the trunk or 1.5 version of django, then that has
experimental support for python 3.

https://docs.djangoproject.com/en/dev/faq/install/#can-i-use-django-with-python-3

If you are trying to learn how to use django, I would strongly
recommend against using an in development version of django, or using
experimental support for python 3, since these can introduce strange
side effects like the one you've noticed that are not fully
documented. OTOH, if you know django and want to make the next release
even better, or improve on that experimental support, dive right in.

Cheers

Tom

Colin Keenan

unread,
Nov 13, 2012, 3:00:57 PM11/13/12
to django...@googlegroups.com
Thanks for the advice. It's true that I'm just learning django, and because I wanted to use python3, I installed via the most up-to-date development source code.  My experience with development versions of stuff has actually been better than the fully supported versions of the same product for some reason. So, I'll be continuing on this path. It's strange that people were experiencing this error over a year ago and didn't mention if they were using python3. Anyone searching on this error in the future will be glad to see the correct answer here if they are using python3.

Colin Keenan

unread,
Nov 13, 2012, 3:10:38 PM11/13/12
to django...@googlegroups.com, teva...@googlemail.com
By the way, I just checked django.VERSION and it's (1, 6, 0, 'alpha', 0)

So, since version 1.6 is supposed to fully support python 3, I should be fine. Obviously, being 'alpha' means I'll run into trouble, but I'll keep checking out new versions as I learn so by the time 1.6 is really out, I'll be all set.

Daniel Roseman

unread,
Nov 14, 2012, 4:13:04 AM11/14/12
to django...@googlegroups.com, teva...@googlemail.com
On Tuesday, 13 November 2012 20:10:38 UTC, Colin Keenan wrote:
By the way, I just checked django.VERSION and it's (1, 6, 0, 'alpha', 0)

So, since version 1.6 is supposed to fully support python 3, I should be fine. Obviously, being 'alpha' means I'll run into trouble, but I'll keep checking out new versions as I learn so by the time 1.6 is really out, I'll be all set.


Django 1.5 isn't even released yet. I've no idea how you've got onto "1.6 alpha", but I doubt it will be released any time soon - it's probably at least 9 months away. You should stick to the released versions rather than running from trunk, and that means Python 2 for now.
--
DR.

Russell Keith-Magee

unread,
Nov 14, 2012, 6:39:02 PM11/14/12
to django...@googlegroups.com
Filling in the blanks here -- If you download Django's trunk branch, it currently reports as 1.6 alpha. When we cut the 1.5 alpha, we created a separate branch for 1.5 development, so that trunk can remain in a live "commit anything" state, rather than our historical "trunk feature freeze" state. 

So, in theory, trunk can currently contain features that won't be included in 1.5; however, in practice, the core team is focussing primarily on the 1.5 release branch, in preparation for the 1.5 beta and final releases.

Yours,
Russ Magee %-)

Manjunath Shivanna

unread,
Jun 20, 2013, 4:10:54 PM6/20/13
to django...@googlegroups.com, mmu...@mshahtech.com
Just restart the python interactive Shell... that should solve your problem

Regards,
Manju

Waqas Javed

unread,
Aug 12, 2013, 7:08:18 AM8/12/13
to django...@googlegroups.com
I was having the same problem my django version was 1.3.1 and python 2.7. There was indentation problem with my code after correcting it my problem was solved.

Brachamul

unread,
Oct 8, 2013, 5:42:43 PM10/8/13
to django...@googlegroups.com
Could someone post what the entire "models.py" file should look like at this point?
I don't think I have indendation problems, but I'm new to both Python & Django, so I can't be sure.
Using Python 2.7.5 and Django 1.4.

Nigel Legg

unread,
Oct 9, 2013, 2:29:09 AM10/9/13
to django...@googlegroups.com
show us your code, and someone might show you where it is wrong, but the tutorial is pretty clear on what you have to do.

Cheers, Nigel
07914 740972



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

Juan Hu

unread,
Jan 10, 2014, 6:44:01 PM1/10/14
to django...@googlegroups.com, mmu...@mshahtech.com
I have same problem with Django 1.6.1 and Python 2.7.5. I tried to restart interactive interpreter by exit() but it still doesn't work :(
Following the tutorial, for Python3 we use _str_ but I am using Python 2.7.5 so I use _unicode_.
Can anyone help to point out the problem and solution? Thanks!

trojactory

unread,
Jan 11, 2014, 9:25:20 AM1/11/14
to django...@googlegroups.com, mmu...@mshahtech.com
Juan,

Are you entering double underscores like '__unicode__'?

Cheers,
Arun

Kai Ren

unread,
Jan 11, 2014, 3:38:16 PM1/11/14
to django...@googlegroups.com, mmu...@mshahtech.com
Just type exit()
then re-enter the shell, it will work.


On Monday, May 16, 2011 7:32:41 AM UTC-5, maaz muqri wrote:

Juan Hu

unread,
Jan 12, 2014, 1:51:34 AM1/12/14
to django...@googlegroups.com, mmu...@mshahtech.com
No, just single underscore

Juan Hu

unread,
Jan 12, 2014, 1:53:00 AM1/12/14
to django...@googlegroups.com, mmu...@mshahtech.com
As I mentioned, I tried this but it doesn't work for me. Is this the python unicode problem? I tried unicode(Poll.objects.all()) and it returns the same

shmengie

unread,
Jan 12, 2014, 9:34:22 AM1/12/14
to django...@googlegroups.com, mmu...@mshahtech.com
trojactory has the right idea.

__unicode__ has two underscores on either side of _ _ unicode _ _

If you don't spell __unicode__ with two underscores on both sides, you are not overriding the default method __unicode__

You are getting the default output for __unicode__ instead of the expected.

Steve Evans

unread,
Mar 27, 2014, 6:06:01 AM3/27/14
to django...@googlegroups.com, mmu...@mshahtech.com
Hi I am having the same issue:

I am using Python 2.7, and Django 1.6.

Here is my code for models.py:

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

# Create your models here.
class Poll(models.Model):
    question = models.CharField(max_length=200)
def __unicode__(self):
return self.question

pub_date = models.DateTimeField('date published')
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

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


I have restarted the shell and this is what I get as a result:

bash-3.2$ python manage.py shell

Python 2.7.6 (default, Dec 19 2013, 06:00:47) 

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

(InteractiveConsole)

>>> from polls.models import Poll, Choice

>>> Poll.objects.all()

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

>>> exit()

bash-3.2$ python manage.py shell

Python 2.7.6 (default, Dec 19 2013, 06:00:47) 

[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin

Type "help", "copyright", "credits" or "license" for more information.

(InteractiveConsole)

>>> from polls.models import Poll, Choice

>>> Poll.objects.all()

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

>>> 

Any help would be great...

Cheers,
Steve

Shai Efrati

unread,
Mar 27, 2014, 6:53:29 AM3/27/14
to django...@googlegroups.com
i wonder if it is just the formatting of the email, but i think you missed spaces before your def. def are hierarchically under classes, so it should be:


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

# Create your models here.
class Poll(models.Model):
    question = models.CharField(max_length=200)
    def __unicode__(self):
return self.question
    pub_date = models.DateTimeField('date published')
    def was_published_recently(self):
          return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

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


Good luck!


Shai.


--
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 http://groups.google.com/group/django-users.

Steve Evans

unread,
Mar 27, 2014, 7:10:41 AM3/27/14
to django...@googlegroups.com
Thankyou! so much...!

It was because i had tabs instead of 4 spaces for the indents.
So basically it was an indentation error but it didn't say that in the console.

I think this will fix a lot of other peoples problems if they are using tabs instead of spaces for indentations.

Cheers.

Matheus Henrique Oliveira

unread,
Mar 27, 2014, 6:56:58 AM3/27/14
to django...@googlegroups.com
Hi Steve.

The __unicode__ method must be within the model class. I took your code and applied the fix. 



--
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/114e5208-81b6-4068-82df-853176ebfd68%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Matheus Henrique Oliveira

Twitter: @matheusho42
+55 16 9-8170.0339
Reply all
Reply to author
Forward
0 new messages