Domanda

When i update the GAE datastore, only after a browser refresh of the page the correct datastore contents is shown:

import os

from google.appengine.ext.webapp import template
from google.appengine.ext import db
import webapp2

#def testkey():
#  return db.Key.from_path('test', 'test')

class TestEntity(db.Model):
  testkey = db.StringProperty(multiline=False)
  testvalue = db.StringProperty(multiline=False)

class TestRefreshProblem(webapp2.RequestHandler):
  def get(self):
    testquery = TestEntity.all()#.ancestor(testkey())
    entities = testquery.run()
    template_values = {
      'entities': entities,
      }
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

class TestRefreshProblemPost(webapp2.RequestHandler):
  def post(self):
    # testEntity = TestEntity(parent=testkey())
    testEntity = TestEntity()
    testEntity.testkey = self.request.get('testkey')
    testEntity.testvalue = self.request.get('testvalue')
    testEntity.put()
    self.redirect('/')

app = webapp2.WSGIApplication([
    ('/', TestRefreshProblem),
    ('/pst', TestRefreshProblemPost)
], debug=True)

and index.html is:

<html>
  <body>
    <table border=0>
        <tr><td width=200>Key</td><td width=200>Value</td></tr>
        {% for entity in entities %}
          <tr>
            <td width=200>{{ entity.testkey|escape }}</td>
            <td width=200>{{ entity.testvalue|escape }}</td>
          </tr>
        {% endfor %}
      </table>
     <form action="/pst" method="post">
      <table>
        <td ><input type="text" name="testkey" size=30/></td>
        <td ><input type="text" name="testvalue" size=30/></td>
        <tr><td><input type="submit" value="Add entity"></td></tr>
      </table>
    </form>
  </body>
</html>

The problem disappears by using a (dummy) ancestor (re: the lines with a #). It seems to me to be strange behaviour ... Can it be solved without an ancestor?

È stato utile?

Soluzione

This is expected behavior because of eventual consistency.

Actually, because you are running the development server, this is only a simulation of eventual consistency - in the real production system, the results won't be quite as predictable. The solution is the same though.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top