Thursday, March 27, 2014

Re: __unicode__() addition not working in basic poll application.

Hi Steve.

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



2014-03-27 7:06 GMT-03:00 Steve Evans <00sevans@gmail.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



On Sunday, 12 January 2014 14:34:22 UTC, shmengie wrote:
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.

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CABd-j%3DS6ib%2B-%2By3VqpNsd3-HZcmcJkDHRp9OMRrYPcHJ6K1wBw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment