In Google App Engine, how do you select entities where an attribute does not exist?
Using the开发者_StackOverflow中文版 python version of GAE and extending models from db.Model, how do you fetch entities where an attribute equals None or does not exist?
#This works
#Fetch 10 entities where duration == 0.0
entities = MyModel.all().filter('duration = ', 0.0).fetch(10)
#This doesn't. How can I do the equivalent?
#Fetch 10 entities where duration == None
entities = MyModel.all().filter('duration = ', None).fetch(10)
You have entities without duration
property (can't be filtered because index can't refer to them) and entities with duration
set to None (can be filtered).
Since you have changed MyModel
schema, you should fix the entities stored without duration
property with something like this:
entities = MyModel.all()
for entity in entities :
if not entity.duration :
entity.duration = None
entity.put()
Have a look to appengine-mapreduce library to accomplish this long running task.
I think the usual way to do this is to add a boolean flag field to your model and filter on that. In your case, that would be something like
class MyModel(db.Model):
duration = db.FloatProperty()
has_duration = db.BooleanProperty(default=False)
# ... etc ...
where you could then do your second query with
entities = MyModel.all().filter('has_duration = ', False).fetch(10)
You'll have to take care to update the has_duration
field when creating/editing your entities, though.
精彩评论