[英]appengine: how can I check if a property from an entity exists in the datastore?
I know it is not possible to query the datastore for missing values (see this question ).我知道无法查询数据存储中的缺失值(请参阅此问题)。
What about from python code? python 代码呢? Is it possible to check if the value from an entity property comes from the datastore or from the default value?
是否可以检查实体属性的值是来自数据存储区还是来自默认值?
Use case:用例:
Model Kind_X has 1000 entities. Model Kind_X有 1000 个实体。 For the property Kind_X.my_property .
对于属性Kind_X.my_property 。
I would like to set my_property to ABC only for those 500 entities that do not have the property.我只想为那些没有该属性的 500 个实体将my_property设置为ABC 。 The 400 entities that have the value None can not be modified.
无法修改值为 None 的 400 个实体。
Note: setting my_property default as ABC is not an acceptable solution.注意:将my_property默认设置为ABC是不可接受的解决方案。
You could iterate over all the entities of a given kind and check it programmatically with:您可以遍历给定类型的所有实体并以编程方式检查它:
entities = Model.all()
for entity in entities :
if not entity.newproperty :
print "Hey, this entity is missing something"
If the number of entities is big, you should use the mapreduce library to avoid timeout.如果实体数量很大,则应使用mapreduce库以避免超时。
It's not possible to do this using the high-level ext.db
framework.使用高级
ext.db
框架无法做到这一点。 You could retrieve data using the lower level google.appengine.api.datastore
framework (documentation is in the docstrings).您可以使用较低级别的
google.appengine.api.datastore
框架检索数据(文档在文档字符串中)。
Why do you need to distinguish these two cases?为什么需要区分这两种情况? It may be that there's a better approach.
可能有更好的方法。
If you don't have lots of data you could do a map reduce and store the keys of the entities you want in a new model that only has a ListProperty holding the keys.如果您没有大量数据,则可以执行 map 减少并将所需实体的密钥存储在新的 model 中,该新 model 只有一个 ListProperty 持有密钥。 It's kind of a dirty hack and works only for less thatn 5k entities.
这是一种肮脏的黑客攻击,仅适用于少于 5k 的实体。 It will also creates lots of metadata so be careful
它还会创建大量元数据,所以要小心
from google.appengine.api import datastore
entity_key = 'ag1lbmdlbG1pbmFzd2VicgoLEgRVc2VyGGIM'
entity = datastore.Get(entity_key)
print 'my_property' in entity
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.