【问题标题】:Can I Query by Key in an App Engine Projection Query?App Engine Projection Query中可以按键查询吗?
【发布时间】:2017-05-11 19:37:26
【问题描述】:

tldr:我可以在 Datastore 投影查询中执行此操作吗?

keyProjectionQuery = Conferences.query(       \
    key=ndb.Key(Conferences, RegistrationId), \
    projection=[Conferences.allConferences])

详细说明: 考虑以下情况,其中 Student 实体是 Conferences 实体的祖先。

  • 学生类中的每个学生只有一个会议类实体。
  • Conferences.allConferences 将学生参加的每个会议都作为重复领域举行。

    班级学生(ndb.model):

    RegistrationId      = ndb.StringProperty()
    Name                = ndb.StringProperty()
    

    类会议(ndb.model):

    allConferences = ndb.StringProperty(repeated=True)
    

    class StudentsForm(messages.message):

    Name                = messages.StringField(1)
    RegistrationId      = messages.StringField(2)
    

    class ConferencesForm(messages.message):

    allConferences = messages.StringField(1, repeated=True)
    

我有一个基于祖先键获取 Conferences.allConferences 的投影查询:-

ancestorProjQuery = Conferences.query(          \
    ancestor=ndb.Key(Students, RegistrationId), \
    projection=[Conferences.allConferences])

我可以在投影查询中使用会议键吗?像这样?

keyProjQuery = Conferences.query(             \
    key=ndb.Key(Conferences, RegistrationId), \
    projection=[Conferences.allConferences])

我试过了,但我得到一个错误:

TypeError: __init__() got an unexpected keyword argument 'key'

第三种选择当然是从 Conferences 中获取整个实体并仅返回 allConferences 属性。

AllConferences = ndb.Key(Conferences, RegistrationId).get()

另外,哪一个会更便宜?

我对 Python、Datastore 和 App Engine 非常陌生。

【问题讨论】:

    标签: python-2.7 google-app-engine google-cloud-datastore


    【解决方案1】:

    试试这个:

    Conferences.query(
        Conferences.key==ndb.Key(Conferences, RegistrationId), projection=["allConferences"]
    )
    

    【讨论】:

    • 这就像一个魅力!谢谢你!有关数据存储成本的任何指示?
    • 一个小小的补充。我必须添加父键才能使其正常工作:query = Conferences.query( Conferences.key== ndb.Key(Conferences, RegistrationId, parent=ndb.Key(Student, RegistrationId)),projection=["allConferences"])
    猜你喜欢
    • 1970-01-01
    • 2012-04-27
    • 2010-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多