Python 在Google App Engine中,如何选择属性不存在的实体?
使用python版本的GAE并从db.Model扩展模型,如何获取属性等于None或不存在的实体Python 在Google App Engine中,如何选择属性不存在的实体?,python,google-app-engine,Python,Google App Engine,使用python版本的GAE并从db.Model扩展模型,如何获取属性等于None或不存在的实体 #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
#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)
我认为通常的方法是向模型中添加一个布尔标志字段,并对其进行过滤。在你的情况下,那可能是
class MyModel(db.Model):
duration = db.FloatProperty()
has_duration = db.BooleanProperty(default=False)
# ... etc ...
在那里,您可以使用
entities = MyModel.all().filter('has_duration = ', False).fetch(10)
但是,在创建/编辑实体时,您必须注意更新
has_duration
字段。您有没有duration
属性的实体(由于索引不能引用它们,所以无法对其进行筛选)和duration
设置为None的实体(可以进行筛选)
由于您已更改了MyModel
schema,因此应使用以下内容修复存储的实体,而不使用duration
属性:
entities = MyModel.all()
for entity in entities :
if not entity.duration :
entity.duration = None
entity.put()
请查看库以完成这项长期运行的任务 我正在寻找一种只查找缺少duration属性的实体的方法。您是否认为唯一的方法是首先在每个实体上创建duration=None?提前感谢。由于您的实体已正确更新,您可以使用查询类从数据存储中获取它们(使用duration=None)。如果没有此修复,我认为您只能使用笨拙的检查“if not entity.duration:…”进行筛选。这不是必需的:一旦使用新定义存储了实体,您就可以在duration=None时进行筛选;在更新之前,它们也没有新的“has_duration”标志。啊,我想我对这个问题有点误解了。