开发者

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? 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. For the property Kind_X.my_property.

  • 500 entities do not have my_property
  • 400 entities my_property is None
  • 100 entities are other values

I would like to set my_property to ABC only for those 500 entities that do not have the property. The 400 entities that have the value None can not be modified.

Note: setting my_property default开发者_如何学Python as ABC is not an acceptable solution.


It's not possible to do this using the high-level ext.db framework. You could retrieve data using the lower level google.appengine.api.datastore framework (documentation is in the docstrings).

Why do you need to distinguish these two cases? It may be that there's a better approach.


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.


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. It's kind of a dirty hack and works only for less thatn 5k entities. 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜