Jesus Benavent
Jesus Benavent

Reputation: 183

Gae ndb structuredproperty

I have something like this:

class Book(ndb.Model):
    date=ndb.DateProperty()
    title=ndb.StringProperty()  
    content=ndb.TextProperty()

class User(ndb.Model):
    username=ndb.StringProperty()
    name=ndb.StringProperty()
    city=ndb.StringProperty() 
    books=ndb.StructuredProperty(Book, repeated=True)

The idea is to have several users and each user have several different books.

Once I have the user object:

user=User.query(User.username=="pepo").get()

I can get the list of books of that user easily:

user.books

but, how can I get, for example, the list of books ordered by date of that user? Or in general any query of the structuredproperty but related with a the user I alrealdy have.

Upvotes: 1

Views: 247

Answers (1)

someone1
someone1

Reputation: 3570

Once you have the user object, you can treat the books property as a normal list. To sort you can do:

sorted_books = sorted(user.books, lambda x: x.date)

You would do the sorting in-memory, not via a query (although I believe you can sort the result of a query on User by a structured property, it won't have any effect on the ordering of the property itself, just the order in which the User objects are returned).

For more information on how to sort lists in Python, view this article.

Upvotes: 4

Related Questions