如何从Google App Engine数据存储区查询中订购引用的对象?

时间:2022-05-28 20:09:37

I have Exhibit objects which reference Gallery objects both of which are stored in the Google App Engine Datastore.

我有Exhibit对象,这些对象引用了Gallery对象,这两个对象都存储在Google App Engine数据存储区中。

How do I order the Exhibit collection on each Gallery object when I get around to iterating over the values (ultimately in a Django template)?

当我绕过迭代值(最终在Django模板中)时,如何在每个Gallery对象上订购Exhibit集合?

i.e. this does not work

即这不起作用


class Gallery(db.Model):
  title = db.StringProperty()
  position = db.IntegerProperty()

class Exhibit(db.Model):
  gallery = db.ReferenceProperty(Gallery, collection_name='exhibits')
  title = db.StringProperty()
  position = db.IntegerProperty()

galleries = db.GqlQuery('SELECT * FROM Gallery ORDER BY position')
for gallery in galleries:
  gallery.exhibits.order('position')

# ... send galleries off the the Django template

When rendered in the template, the galleries are correctly ordered but the exhibits are not.

在模板中渲染时,画廊的排序是正确的,但展品不是。

1 个解决方案

#1


4  

Instead of relying on the collection property App Engine creates, you need to construct your own query:

您需要构建自己的查询,而不是依赖App Engine创建的集合属性:

exhibits = Exhibit.all().filter("gallery =", gallery).order("position")

exhibit = Exhibit.all()。filter(“gallery =”,gallery).order(“position”)

Or equivalently, in GQL:

或者等效地,在GQL中:

exhibits = db.GqlQuery("SELECT * FROM Exhibit WHERE gallery = :1 ORDER BY position", gallery)

exhibit = db.GqlQuery(“SELECT * FROM Exhibit WHERE gallery =:1 ORDER BY position”,gallery)

If you want to be able to do this from inside the template, rather than passing in a list-of-lists of exhibits, you can define a simple method on the Gallery object that executes this query, and reference it from the template (Eg, {{gallery.exhibits_by_position}} will execute exhibits_by_position() on the Gallery object, which can then perform the query above).

如果您希望能够从模板内部执行此操作,而不是传入展示列表列表,则可以在Gallery对象上定义执行此查询的简单方法,并从模板中引用它(例如,{{gallery.exhibits_by_position}}将在Gallery对象上执行exhibit_by_position(),然后可以执行上面的查询)。

If you're concerned about the speed implications of this, don't worry: The collection property App Engine creates is simply syntactic sugar for this.

如果您担心速度影响,请不要担心:App Engine创建的集合属性只是语法糖。

#1


4  

Instead of relying on the collection property App Engine creates, you need to construct your own query:

您需要构建自己的查询,而不是依赖App Engine创建的集合属性:

exhibits = Exhibit.all().filter("gallery =", gallery).order("position")

exhibit = Exhibit.all()。filter(“gallery =”,gallery).order(“position”)

Or equivalently, in GQL:

或者等效地,在GQL中:

exhibits = db.GqlQuery("SELECT * FROM Exhibit WHERE gallery = :1 ORDER BY position", gallery)

exhibit = db.GqlQuery(“SELECT * FROM Exhibit WHERE gallery =:1 ORDER BY position”,gallery)

If you want to be able to do this from inside the template, rather than passing in a list-of-lists of exhibits, you can define a simple method on the Gallery object that executes this query, and reference it from the template (Eg, {{gallery.exhibits_by_position}} will execute exhibits_by_position() on the Gallery object, which can then perform the query above).

如果您希望能够从模板内部执行此操作,而不是传入展示列表列表,则可以在Gallery对象上定义执行此查询的简单方法,并从模板中引用它(例如,{{gallery.exhibits_by_position}}将在Gallery对象上执行exhibit_by_position(),然后可以执行上面的查询)。

If you're concerned about the speed implications of this, don't worry: The collection property App Engine creates is simply syntactic sugar for this.

如果您担心速度影响,请不要担心:App Engine创建的集合属性只是语法糖。