[Django]-Django 1.9 tutorial __str__ () not working

7πŸ‘

βœ…

You most likely do not have question_text field in your Question model. You may have deleted it when you added the __str__ method definition.

Check that you have this:

class Question(models.Model):
    question_text = models.CharField(max_length=200) # <--- Double check you have this

    def __str__(self):
        return self.question_text
πŸ‘€bakkal

0πŸ‘

If you are following the Django documentation, then it is some typo at your end. Just redo the model part and run the makemigration and migrate cmd. It should work.
I faced the same issue and then I just copied and pasted the code from Django documentation and it worked.

πŸ‘€Samcs099

0πŸ‘

Do this.

def __repr__ (self):
    return self.title

restart the shell/session.

then check.

πŸ‘€TrickyJ

0πŸ‘

I was also getting the same problem but then I checked I forget to adddouble inverted comma instead of single _ in __str__

0πŸ‘

Maybe you are getting the error because you don’t have proper indentation. I got the same error when I didn’t have proper indentation. Now it’s correct. Inside the class you should declare the function as following:

class Position(models.Model):
   title = models.CharField(max_length=50)
   def __str__(self):
       return self.title
πŸ‘€Akshay Babu

Leave a comment