0
votes

tldr: Can I do this in a Datastore projection query?

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

In detail: Consider the following, where Students entity is an ancestor of Conferences entity.

  • There is only one entity in Conferences kind for every student in Students kind.
  • Conferences.allConferences holds every conference that a student has attended as a repeated field.

    class Students(ndb.model):

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

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

I have a projection query that gets Conferences.allConferences based on ancestor key:-

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

Can I use Conferences key in a projection query? Something like this?

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

I tried it, but I get an error:

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

The third option is ofcourse to get the entire entity from Conferences and return only the allConferences property.

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

Also, which of these will be cheaper?

I am very new to Python, Datastore and App Engine.

1

1 Answers

0
votes

Try this:

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