Hi guys, I'm working on Writing your first Django app, part 2 tutorial, and I tried _str_() function and it doesn't give me the same result

Viewed 51

this is my Question class in models.py :

class Question(models.Model):
     question_text = models.CharField(max_length=200)
     pub_date = models.DateTimeField('date published')
     def _str_(self):
         return self.question_text

when I execute : Question.objects.all() it gives me : <QuerySet [<Question: Question object (1)>, <Question: Question object (2)>]> any ideas ???

2 Answers

you need to use double underscore(__) not single(_) for your dunder method

def __str__(self):
    return self.question_text

it should be

class Question(models.Model):
     question_text = models.CharField(max_length=200)
     pub_date = models.DateTimeField('date published')
     def __str__(self):
         return self.question_text
Related