简体   繁体   English

appengine:如何检查数据存储中是否存在实体的属性?

[英]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

  • 500 entities do not have my_property 500 个实体没有my_property
  • 400 entities my_property is None 400 个实体my_property为无
  • 100 entities are other values 100 个实体是其他值

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.

相关问题 如何检查Google App Engine数据存储区中特定列中是否存在特定值?(Python) - How can I check if a particular value exists in a particular column in google app engine datastore?(python) 如何将数据从本地Appengine数据存储移动到远程数据存储? - How do I move data from local appengine datastore to remote datastore? 无法从数据存储区实体访问ID属性 - Unable to access ID property from a datastore entity 如何从index.html中的模板返回appengine数据存储区对象的键? - How do I return the key of an appengine datastore object from the template in index.html? 如何在运行时确定类型的GAE数据存储区中创建实体? - How can I create an entity in the GAE datastore of a type determined at runtime? 如何检查gae数据存储区中是否存在实体? - How to check if entity exist in gae datastore? 如何从数据存储刷新NDB实体? - How do I refresh an NDB entity from the datastore? 如何在没有在Google App Engine中读取数据存储的情况下从实体实例获取引用的实体属性的键名? - How to get to the key name of a referenced entity property from an entity instance without a datastore read in google app engine? 检查Google App Engine数据存储区实体是否具有特定属性 - Check if Google App Engine datastore entity has a particular property 我如何观察appengine的数据存储模型? - how do I instrospect appengine's datastore models?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM