【发布时间】:2017-09-19 15:27:42
【问题描述】:
我正在尝试在 Python 中编写一个查询,使用 数据存储 API GPA大于且等于3.2且出生年份小于1998的学生姓名
但是,它返回错误:BadFilterError: invalid filter: Only one property per query may have inequality filters (<, >=, >, <=)..
但是,我使用的是文档中提到的方法:
https://cloud.google.com/appengine/docs/standard/python/datastore/queryclass#Query_filter
https://cloud.google.com/appengine/docs/standard/python/datastore/queries
代码:
from google.appengine.ext import db
import datetime
import webapp2
class Student(db.Model):
first_name = db.StringProperty()
last_name = db.StringProperty()
gpa = db.FloatProperty()
birth_year = db.IntegerProperty()
class MainPage(webapp2.RequestHandler):
def get(self):
student1 = Student(first_name = 'Alex' , last_name = 'Karev', gpa = 3.5 , birth_year = 1996 )
student1.put()
student2 = Student(first_name = 'Susannah', last_name = 'Walpole',gpa = 3.45, birth_year = 1997 )
student2.put()
self.response.write('<p>Student1 entity, key = %s</p>'
% student1.key())
self.response.write('<p>Student2 entity, key = %s</p>'
% student2.key())
q = db.Query(Student)
q = Student.all()
q.filter('gpa >', 3.2)
q.filter('birth_year <', 1998)
results = q.fetch(5)
self.response.write('<p>Executing the query with fetch()...</p>')
for e in results:
self.response.write('<p>Found result: Last Name=%s'
% (e.last_name))
app = webapp2.WSGIApplication([('/', MainPage)], debug=True)
而且,当我使用 GQL 而不是数据存储 API 时,我遇到了同样的错误。 代码:
q = Student.gql('WHERE gpa > 3.2 ' +'AND birth_year < 1998 ' +'ORDER BY gpa ASC, birth_year DESC')
【问题讨论】:
-
是的,但是我的模型不同,我已经尝试过了,但它不起作用。
标签: python google-app-engine google-cloud-datastore google-cloud-platform