【问题标题】:Google App Engine - Search for stringProperty in pythonGoogle App Engine - 在 python 中搜索 stringProperty
【发布时间】:2016-05-24 23:36:43
【问题描述】:

我是新的 Google App Engine,所以我从 Python 的 HelloWorld 和留言簿教程开始学习。

现在我想在我的班级中查询一个特定的字符串,但我在 GAE 文档中找不到合适的文档。

我有以下:

class song_key(genre)
    #We use genre as the key for a Song entity
    return ndb.Key('Repository',genre)

class Singer(ndb.Model):
    name = ndb.StringProperty()
    birthday = ndb.DateProperty()

class Song(ndb.Model):
    author = ndb.StructuredProperty(Singer)
    title = ndb.StringProperty()
    release = ndb.DateProperty()
    dateAdd = ndb.DateTimeProperty(auto_now_add=True)

假设它们都属于同一个“流派”,我想找到所有作者 == 'John Lennon' 的歌曲

我尝试了一些类似的东西:

q = Song.query((Song.author.name == "John Lennon'), ancestor=song_key(genre)).order(-Song.data)
results = q.all()

但这显然不是执行此查询的方法。

有人可以帮我理解 Google App Engine 中的查询吗?实现这样的 stringProperty 查询的好方法是什么?

我经历过https://cloud.google.com/appengine/docs/python/ndb/queries 或https://cloud.google.com/appengine/docs/python/datastore/queryclass 但我在理解整个概念时遇到了一些问题^^

编辑:这是两个查询的结果:

Query(kind='Song', ancestor=Key('Repository', 'Pop'), orders=...)

[Song(key=Key('Repository', 'Pop', 'Song', 5733953138851840), author=Singer(name=u'John Lennon'), date=datetime.datetime(2016, 2, 13, 22, 59, 31, 766373),title=u'Come Together'), Song(key=Key('Repository', 'Pop', 'Song', 5556931766779904), author=Singer(name=u'Bruno Mars'), date=datetime.datetime(2016, 2, 13, 16, 15, 58, 584174), title=u'Uptown Funk), Song(key=Key('Repository', 'Pop', 'Song', 6119881720201216), author=Singer(name=u'Katty Perry'), date=datetime.datetime(2016, 2, 13, 16, 15, 30, 915749), title=u'Teenage Dream')]

我有三个条目。

当我想按作者姓名过滤时:

Query(kind='Song', ancestor=Key('Repository', 'Pop'), filters=FilterNode('author.name', '=', 'John Lennon'), orders=...)

[]

空列表!

谢谢!

【问题讨论】:

  • 为什么“显然”不是这样?如果你想查询歌手属性,为什么要让它们 indexed=False?
  • 类 song_key() 应该做什么。这有点奇怪
  • 我是从留言本教程cloud.google.com/appengine/docs/python/gettingstartedpython27/… 开始的,但你完全正确。我将歌手属性编入索引:) song_key() 是音乐实体的“流派”。就像留言簿教程中的“guestbook_key”一样,我使用歌曲键来收集不同“流派”类别下的所有歌曲条目。我还添加了我的查询结果。

标签: python string google-app-engine properties


【解决方案1】:

好的,我找到了一种解决问题的 Python 方法: 我查询特定流派的所有歌曲,然后检查我要查找的歌手是否属于此列表。

q = Song(ancestor=repository_key(genre))
results = p.fetch()

songs = []
for result in results:
    if author in result.author.name:
        songs.append(result)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-15
    • 2012-07-10
    相关资源
    最近更新 更多