简体   繁体   English

Gae ndb结构化属性

[英]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. 或者一般来说,对结构化属性的任何查询,但与我实际拥有的用户有关。

Once you have the user object, you can treat the books property as a normal list. 获得用户对象后,可以将books属性视为普通列表。 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). 您可以在内存中进行排序,而不是通过查询进行排序(虽然我相信您可以通过结构化属性对User的查询结果进行排序,但它不会对属性本身的排序产生任何影响,只是顺序其中返回User对象)。

For more information on how to sort lists in Python, view this article . 有关如何在Python中对列表进行排序的更多信息,请查看此文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM