python webapp2数据存储区查询

时间:2018-07-17 09:40:37

标签: python webapp2 datastore

我正在尝试从数据存储中获取所有由“作者”过滤的“ DepAnswers”。

我的问题是,“ DepAnswers”已正确存储在本地数据库中,但是我无法从“作者”过滤的数据存储中按“创建”降序检索所有“ DepAnswers”。

我通过以下代码得到的是:

depAns = [] 
#How should I structure the query
depAns = DepAnswers.query()
depAns = depAns.filter(ndb.GenericProperty("author") == users.get_current_user().nickname())
depAns = depAns.get()

获取“作者”的“ depAns”列表

class Handler(webapp2.RequestHandler):
    def write(self, *a, **kw):
        self.response.out.write(*a, **kw)
    def render_str(self, template, **params):
        t = jinja_env.get_template(template)
        return t.render(params)
    def render(self, template, **kw):
        self.write(self.render_str(template, **kw))

class DepAnswers(ndb.Model):
    author = ndb.TextProperty()    
    num = ndb.IntegerProperty()
    created = ndb.DateTimeProperty(auto_now_add = True)

class DepQFormSubmitResult(Handler):
    def post(self):
        depAns = DepAnswers.query()
        depAns = depAns.filter(ndb.GenericProperty("author") == users.get_current_user().nickname())

        depAns = depAns.get()

        response_string = """<html><body>
            In DepQFormSubmitResult  <br>
                               </body></html>"""
        self.response.headers["Content-Type"] = "text/html"
        self.response.write(response_string)
        response_string = """<html><body> depAns = {}  <br>
                              </body></html>""".format(str(depAns))                               
        self.response.headers["Content-Type"] = "text/html"
        self.response.write(response_string)
        response_string = """<html><body> depAns = {}  <br>
                              </body></html>""".format(depAns)

0 个答案:

没有答案