I changed the properties of question_text and saved
Then add the __str__() method and query all Questions again,
My above code is implemented according to this
http://www.yiibai.com/django/... I just learned it. My steps are the same as this tutorial, which is to add __str__ () method, the correct display of the tutorial is as follows:
But I tested it myself and entered the command, but I couldn't see the records after I changed it. For example, I changed
q.question_text = "What's up?"
q.save()
After saving the changes, run the following command
Question.objects.all()
The result is as shown below:
What is the reason for this? —Django1.9, the database is the default sqlite3
def __str__ This should be the class method of the model Question. This method determines the return when you query. You define return self.question_text, so when you query the object, it will return the question_text attribute of the object, but your writing The format is wrong. If this method is defined outside the class, it becomes a single function and has nothing to do with the class. When you query, a Question object will be returned by default.
Thanks to tianren124 for the answer, the problem has been solved.
First you need to modify models.py:
models.py
After changing the model.py code above, save it, open cmd, and re-enter it
Enter simultaneously
You can see that, unlike the results in the previous question, when Question.objects.all() is entered, the running result is the value after I change q.question_tex "What's up?
Solution:
1. Modify models.py
should be placed in
Also put
In front of me, I don’t quite understand why.
2. Pay attention to the indentation:
The expression may not be very clear, please correct me