How can I order referenced objects from a Datastore Google App Engine request?

I have objects Exhibitthat reference objects Gallery, both of which are stored in the Google App Engine datastore.

How to arrange a collection Exhibitfor each object Gallerywhen I get around to iterating over values ​​(ultimately in a Django template)?

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 rendering in the template, the galleries are correctly ordered, but the exhibits are not.

+3
source share
1 answer

Instead of relying on the App Engine collection property, you should create your own query:

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

, , GQL:

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

, , Gallery, , template (, {{gallery.exhibits_by_position}} exhibits_by_position() Gallery, ).

, : App Engine .

+4

Source: https://habr.com/ru/post/1705481/


All Articles