python - TypeError: coercing to Unicode: need string or buffer, NoneType found -
i'm wading throught django tutorial, part 2: https://docs.djangoproject.com/en/1.9/intro/tutorial02/ , in place attach several choices 1 question
# display choices related object set -- none far. >>> q.choice_set.all() []
i got error:
/home/paulmad/env/local/lib/python2.7/site-packages/ipython/lib/pretty.pyc in _repr_pprint(obj, p, cycle) 692 """a pprint redirects normal repr function.""" 693 # find newlines , replace them p.break_() --> 694 output = repr(obj) 695 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 unicode: need string or buffer, nonetype found
here models:
from __future__ import unicode_literals import datetime django.db import models django.utils import timezone django.utils.encoding import python_2_unicode_compatible # create 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 resolve problem?
in 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 ''
Comments
Post a Comment