Google App Engine:计算数据存储区中的条目

时间:2014-07-10 06:23:39

标签: python google-app-engine

我正在使用Google App Engine和Python作为后端语言处理Web应用程序。我需要根据一定数量的参数计算数据存储区中的条目总数。

以下是相同的代码:

main.py

import os
import webapp2
import jinja2
from google.appengine.ext import db
from random import randint

template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir), autoescape=True)

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))

#databases

class Survey(db.Model):
    vam_id = db.StringProperty()
    subject = db.StringProperty(required = True)
    description = db.TextProperty(required = True)
    created = db.DateTimeProperty(auto_now_add = True)
    q1 = db.TextProperty(required = True)
    o11 = db.TextProperty(required = True)
    o12 = db.TextProperty(required = True)
    o13 = db.TextProperty(required = True)
    q2 = db.TextProperty(required = True)
    o21 = db.TextProperty(required = True)
    o22 = db.TextProperty(required = True)
    o23 = db.TextProperty(required = True)
    q3 = db.TextProperty(required = True)
    o31 = db.TextProperty(required = True)
    o32 = db.TextProperty(required = True)
    o33 = db.TextProperty(required = True)
    q4 = db.TextProperty(required = True)
    o41 = db.TextProperty(required = True)
    o42 = db.TextProperty(required = True)
    o43 = db.TextProperty(required = True) 
    q5 = db.TextProperty(required = True)
    o51 = db.TextProperty(required = True)
    o52 = db.TextProperty(required = True)
    o53 = db.TextProperty(required = True)

class Votes(db.Model):
    vam_id = db.StringProperty(required = True)
    q1_opt = db.TextProperty(required = True)
    q2_opt = db.TextProperty(required = True)
    q3_opt = db.TextProperty(required = True)
    q4_opt = db.TextProperty(required = True)
    q5_opt = db.TextProperty(required = True)


"""a = Survey(vam_id = "01", subject = "Metro", description = "A survey on Mumbai Metro",
            q1 = "How do you find rates fixed for Metros?", 
            o11 = "Appropriate", o12 = "Somewhat appropriate", o13 = "Inappropriate",
            q2 = "How satisfied are you with the customer service",
            o21 = "Extremely satisfied", o22 = "Moderately satisfied", o23 = "Not satisfied",
            q3 = "How satisfied are you with the interior of Mumbai Metro?",
            o31 = "Extremely satisfied", o32 = "Moderately satisfied", o33 = "Not satisfied",
            q4 = "Do you think travelling by Metro is more convenient than other transport?",
            o41 = "Yes", o42 = "No", o43 = "Can't say",
            q5 = "How will you rate Mumbai Metro?",
            o51 = "Very good", o52 = "Good", o53 = "Bad")  

a.put()         """  

class MainPage(Handler):
    def get(self):
        self.render("homepage.html")

class NewSurvey(Handler):
    def get(self):
        self.render("newsurvey.html")
    def post(self):
        vam_id = str(randint(1, 500))
        subject = self.request.get("title")
        description = self.request.get("description")
        q1 = self.request.get("q1")
        o11 = self.request.get("o11")
        o12 = self.request.get("o12")
        o13 = self.request.get("o13")
        q2 = self.request.get("q2")
        o21 = self.request.get("o21")
        o22 = self.request.get("o22")
        o23 = self.request.get("o23")
        q3 = self.request.get("q3")
        o31 = self.request.get("o31")
        o32 = self.request.get("o32")
        o33 = self.request.get("o33")
        q4 = self.request.get("q4")
        o41 = self.request.get("o41")
        o42 = self.request.get("o42")
        o43 = self.request.get("o43") 
        q5 = self.request.get("q5")
        o51 = self.request.get("o51")
        o52 = self.request.get("o52")
        o53 = self.request.get("o53")

        a = Survey(vam_id = vam_id, subject = subject, description = description, q1 = q1,
                    o11 = o11, o12 = o12, o13 = o13, q2 = q2,
                    o21 = o21, o22 = o22, o23 = o23, q3 = q3,
                    o31 = o31, o32 = o32, o33 = o33, q4 = q4,
                    o41 = o41, o42 = o42, o43 = o43, q5 = q5,
                    o51 = o51, o52 = o52, o53 = o53)
        a.put()            

class Existing(Handler):
    def get(self):
        surveys = Survey.all()
        self.render("existingsurvey.html", surveys = surveys)

class PermaSurvey(Handler):
    def get(self):
        vam = self.request.get("vam_id")
        su = Survey.all().filter("vam_id =", vam).get()
        self.render("perma.html", su = su)
    def post(self):
        vam = self.request.get("vam_id")
        q1_opt = self.request.get("q1")
        q2_opt = self.request.get("q2")
        q3_opt = self.request.get("q3")
        q4_opt = self.request.get("q4")
        q5_opt = self.request.get("q5")

        v = Votes(vam_id = vam, q1_opt = q1_opt, q2_opt = q2_opt, q3_opt = q3_opt,
                    q4_opt = q4_opt, q5_opt = q5_opt)  
        v.put()

        q11count = Votes.all().filter("vam_id = ", vam)
        q1count = q11count.filter("q1_opt = ", q1_opt).count()
        self.response.write(q1count)

app = webapp2.WSGIApplication([('/', MainPage),
                               ('/new', NewSurvey),
                               ('/existing', Existing),
                               ('/perma', PermaSurvey)], debug=True)

这是'投票'数据存储区看起来像:

enter image description here

但是,我们的main.py文件中的以下查询始终会将计数返回为' 0':

q11count = Votes.all().filter("vam_id = ", vam)
q1count = q11count.filter("q1_opt = ", q1_opt).count()
self.response.write(q1count)

我的查询语法有什么问题?

1 个答案:

答案 0 :(得分:1)

您似乎试图过滤TextProperty那是不可能的。下面的代码段不会按预期工作。

filter("q1_opt = ", q1_opt)

如果您需要对该属性进行过滤,则可能需要将其类型更改为其他类型,例如StringProperty

  

与StringProperty不同,TextProperty值的长度可超过500个字符。但是,TextProperty值未编入索引,因此无法在过滤器或排序顺序中使用。