Check if a field is present in an entity
I have updated my model in Datastore so now it has an additional field. Now I have entities with and without that field but I need to add this field to all entities that don't yet have it. Idea is to get entities in a function without that field and add it. So, I wonder how I can filter such entities in Datastore requ开发者_如何学Pythonests?
You cannot filter for property non-existence. Every query must be satisfied by an index, and there's no "negative index" of entities that lack a given property.
Generally, you'll need to iterate over all entities, and just ignore the ones that already have the property.
It might not be so easy, but I guess it's possible.
You cannot get the entities without a property:
No Use of Filters That Match Entities Missing a Property
It is not possible to query for entities that are missing a given property. One alternative is to create a fixed (modeled) property with a default value of None, then create a filter for entities with None as the property value.
You could get all filtering on that specific property:
Filtering Or Sorting On a Property Requires That the Property Exists
If a property has a query filter condition or sort order, the query returns only those datastore entities that have a value (including null) for that property. Entities of a kind need not have the same properties. A filter on a property can only match an entity with a value for that property. If an entity has no value for a property used in a filter or sort order, that entity is omitted from the index built for the query.
My suggestion is to get the keys from all and subtract the list with keys from entities with that property.
Reference: http://code.google.com/intl/en-US/appengine/docs/python/datastore/queries.html#Overview
精彩评论