NDB query with computed property returns empty list
I am trying to query a ndb model with a computed property, but it returns an empty list. This answer says that I should be able to query computed properties, and so the docs . What am I doing wrong?
from django.template import defaultfilters
class Video(models.SfxModel):
title = ndb.StringProperty()
slug = ndb.ComputedProperty(
lambda self: str(defaultfilters.slugify(self.title)) )
In the interactive console
from app.lib.videos import Video
slug = Video.query().get().slug
print slug
# => "some-dasherized-string"
print Video.query(Video.slug == slug).fetch()
# => []
0
source to share
1 answer
the "problem" you are running into is the ultimate consistency specified for non ancestral queries .
what you see is perfectly normal for a highly replicated datastore. when you place an object and request it immediately after it may not be replicated in all datacenters so that it cannot be found.
if you want this to work you need to use entity groups by adding a parent to the entity. it can be an entity key or a constructed key that does not belong to any stored object.
it works:
class Video(ndb.Model):
title = ndb.StringProperty()
slug = ndb.ComputedProperty(lambda self: self.title.replace(' ', '-'))
v = Video(parent = ndb.Key(Video, 'xxx'), title = 'foo bar')
v.put()
print Video.query(Video.slug == v.slug, ancestor = ndb.Key(Video, 'xxx')).get()
0
source to share