1👍
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 ''
- Correct way to add fields to a model on django
- Pycharm not recognizing Django project
- Password Reset Confirm don`t work
- Trouble to change user.is_staff 's value
0👍
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.
Source:stackexchange.com