我正在尝试使用数据存储区API在Python中编写查询,最后获取不超过5个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')
答案 0 :(得分:0)
使用数据存储区时,不能将多个变量与不等式过滤器一起使用。如果你真的需要,你可以只使用'开始'时间过滤,并且仍然可以获得非常准确的结果。
calitem = self.appointments.filter("begin >= ", start).filter("begin <= ", end).fetch(limit=10)
答案 1 :(得分:0)
q.filter('gpa >', 3.2)
q.filter('birth_year <', 1998)
这不是有效的数据存储区查询,因为它有多个不等式过滤器,并返回错误:BadFilterError: invalid filter: Only one property per query may have inequality filters (<, >=, >, <=)..
查询的结果必须全部出现在索引中的连续行中,并且没有可能的索引可以完全使用连续行来满足此查询。
所以,我只使用q.filter('gpa >', 3.2)
并删除q.filter('birth_year <', 1998)
来解决了这个问题,并使用if语句来过滤打印输出的for look中的birth_year:
self.response.write('<p>Executing the query with fetch()...</p>')
count = 0
for e in q:
if e.birth_year < 1998:
self.response.write('<p>Found result: Last Name=%s'
% (e.last_name))
count += 1
if count > 4:
break