简体   繁体   中英

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 as ABC is not an acceptable solution.

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM