Reputation: 1913
I'm wading throught the Django tutorial, part 2: https://docs.djangoproject.com/en/1.9/intro/tutorial02/ and in a place where we attach several choices to one question
# Display any choices from the related object set -- none so far.
>>> q.choice_set.all()
[]
I got an error:
/home/paulmad/env/local/lib/python2.7/site-packages/IPython/lib/pretty.pyc in _repr_pprint(obj, p, cycle)
692 """A pprint that just redirects to the normal repr function."""
693 # Find newlines and replace them with p.break_()
--> 694 output = repr(obj)
695 for idx,output_line in enumerate(output.splitlines()):
696 if idx:
/home/paulmad/env/local/lib/python2.7/site-packages/django/db/models/query.pyc in __repr__(self)
235 if len(data) > REPR_OUTPUT_SIZE:
236 data[-1] = "...(remaining elements truncated)..."
--> 237 return repr(data)
238
239 def __len__(self):
/home/paulmad/env/local/lib/python2.7/site-packages/django/db/models/base.pyc in __repr__(self)
457 def __repr__(self):
458 try:
--> 459 u = six.text_type(self)
460 except (UnicodeEncodeError, UnicodeDecodeError):
461 u = '[Bad Unicode data]'
TypeError: coercing to Unicode: need string or buffer, NoneType found
Here are my models:
from __future__ import unicode_literals
import datetime
from django.db import models
from django.utils import timezone
from django.utils.encoding import python_2_unicode_compatible
# Create your models here.
@python_2_unicode_compatible
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 __unicode__(self):
return unicode(self.quesion_text) or u''
def was_published_recently(self):
return self.pub_date >= timezone.now() - datetime.timedelta(days=1)
@python_2_unicode_compatible
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):
self.choice_text
def __unicode__(self):
return unicode(self.choice_text) or u''
Django 1.9.1, how to resolve this problem?
Upvotes: 0
Views: 2685
Reputation: 51938
In your model, update method called __str__(self)
.
class Choice(models.Model):
choice_text = models.CharField(max_length=255, null=True)
def __str__(self):
return self.choice_text if self.choice_next else ''
Upvotes: 1
Reputation: 308769
You are using @python_2_unicode_compatible
, so you should there is no need to define __unicode__
and __str__
.
You only need to define __unicode__
if you are using Python 2 without the @python_2_unicode_compatible
decorator.
You should change your models to something like:
@python_2_unicode_compatible
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)
@python_2_unicode_compatible
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):
self.choice_text
You shouldn't need code like unicode(self.choice_text) or u''
in your methods - the CharField
should default to the empty string when a value isn't set.
Upvotes: 0
Reputation: 1913
In fact, it was needed only to add return to str before self.choice_text.
Upvotes: 0