search

Home  >  Q&A  >  body text

python - Django encountered problems operating the database and could not query the updated data

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

PHP中文网PHP中文网2711 days ago889

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-06-30 09:57:15

    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.

    reply
    0
  • typecho

    typecho2017-06-30 09:57:15

    Thanks to tianren124 for the answer, the problem has been solved.
    First you need to modify models.py:
    models.py

    # Create your models here.
    class Question(models.Model):
    
        def __str__(self):
            return self.question_text
        question_text = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
        
    class Chioce(models.Model):
        def __str__(self):
            return self.choice_text
        question = models.ForeignKey(Question, on_delete=models.CASCADE)
        choice_text = models.CharField(max_length=200)
        votes = models.IntegerField(default=0)
        # 每个模型是django.db.models.Model类的子类
       
    #def was_published_recently(self):
            #return self.pub_date >= timezone.now() - datetime.timedelta(days=1)

    After changing the model.py code above, save it, open cmd, and re-enter it

    C:\Users\Administrator\AppData\Local\Programs\Python\Python35\myproject>python m
    anage.py runserver

    Enter simultaneously

    
        C:\Users\Administrator\AppData\Local\Programs\Python\Python35\myproject>python m
    anage.py shell
    Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AM
    D64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    (InteractiveConsole)
    >>> import django
    >>> django.setup()
    >>> from polls.models import Question, Chioce
    >>> Question.objects.all()
    [<Question: What's up?>, <Question: What's up?>, <Question: What's up?>]
    >>>
    
    

    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

    def __str__(self):
            return self.question_text
        

    should be placed in

    
        question_text = models.CharField(max_length=200)
        pub_date = models.DateTimeField('date published')
    
     def __str__(self):
            return self.choice_text

    Also put

    question = models.ForeignKey(Question, on_delete=models.CASCADE)
        choice_text = models.CharField(max_length=200)
        votes = models.IntegerField(default=0)

    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

    reply
    0
  • Cancelreply