Friday, July 1, 2016

Re: def__str__(self) not working properly

Hm, not much can go wrong there. Just to be sure, did you start the shell after you added the __str__ method?

On Friday, July 1, 2016 at 5:52:12 AM UTC-4, gary719_list1 wrote:
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/89ad85cb-64e2-4aac-b6db-5d552c543c2e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment