Friday, July 1, 2016

Re: def__str__(self) not working properly


2016-07-01 17:50 GMT+08:00 Gary Roach <gary719_list1@verizon.net>:
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.

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

--
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CANURRHCiVx3%2BQC_3JWvUOyJPS2amc1naSBLnfwsVaM-6DW0Mog%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment