具有计算属性的NDB查询返回一个空白列表 [英] NDB Query with Computed Property returns a blank list

查看:102
本文介绍了具有计算属性的NDB查询返回一个空白列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用计算属性查询ndb模型,但是它返回一个空列表。 这个答案表明我应该可以查询计算的属性,所以也可以使用中的google.com/appengine/docs/python/ndb/queries#introrel =nofollow noreferrer。我做错了什么?

I'm trying to query an ndb Model with a computed property, but it's returning an empty list. This answer suggests that I should be able to query computed properties and so do 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)) )

在互动控制台中

from app.lib.videos import Video

slug = Video.query().get().slug
print slug
# => "some-dasherized-string"
print Video.query(Video.slug == slug).fetch()
# => []


推荐答案

你所遇到的问题 最终一致性为非祖先查询

你看到的是高复制是完全正常的数据存储。
当您放置一个实体并查询它是正确的它可能是它没有复制在所有数据中心,所以它找不到。

the 'issue' you are having is the eventual consistency given for non ancestor queries.
what you are seeing is completely normal for the high replication datastore. when you put an entity and query for it right after it could be that its not replicated over all datacenters so it could not be found.

如果你想这样,您必须通过向实体添加父项来使用实体组。
这可以是不属于任何存储实体的实体密钥或构造的密钥。

if you want this to work you have to use entity groups by adding a parent to an entity. this can be an entity key or a constructed key that does not belong to any stored entity.

这样做:

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()

这篇关于具有计算属性的NDB查询返回一个空白列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆