jbro
jbro

Reputation: 75

GAE NDB Accessing sub-instances (fields) within structured repeated list

I am having difficulties with accessing an instance within a structured list.
Below is my structured list:

class FavFruits(ndb.Model):
    fruit    = ndb.StringProperty()
    score    = ndb.IntegerProperty()
    comment  = ndb.TextProperty()

class UserProfile(ndb.Model):
    uid            = ndb.StringProperty(required=True)
    password       = ndb.StringProperty(required=True)
    firstName      = ndb.StringProperty(required=True)
    favFruits      = ndb.StructuredProperty(FavFruits, repeated=True)

I want to display score under FavFruits entity.

I tried UserProfile.favFruits.score with no luck.

I also tried UserProfile.favFruits[index].score, which worked, but now requires looping and I would like to avoid it.

Ultimately, I want to do the following logic:

  1. if UserProfile.uid == userEntering then user enters fruit name

  2. if UserProfile.favFruits.fruit == fruitName (user entered) then display UserProfile.favFruits.score and UserProfile.favFruits.comments for UserProfile.favFruits.fruit specified by user.

  3. Lastly, I would like to display all the fruit/scores that user enters. Say, user entered "apple" and "orange" for fruit names, then I want to loop, for example (along this line):

for x in fruitNames print x print UserProfile.favFruits.score.query(UserProfile.favFruits.fruit == x)

Is this possible? Seemingly trivial task, but I cannot figure this out..

Thank you in advance!

Upvotes: 1

Views: 128

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

Your requirements are contradictory. If you don't want to loop, then don't use repeated=True. But then you won't be able to store more than one for each entity. There's no possible way to have multiple things without looping or indexing.

Upvotes: 3

Related Questions