开发者

Need to order by properties in the reference property, any good solution?

开发者 https://www.devze.com 2023-01-08 15:58 出处:网络
Say I have 2 kind: class Account(db.Model): name = db.StringProperty() create_time = db.DataTimeProperty()

Say I have 2 kind:

class Account(db.Model):
  name = db.StringProperty()

  create_time = db.DataTimeProperty()
  last_login = db.DateTimeProperty()
  last_update = db.DataTimeProperty()


class Relationship(db.Model)
  owner = db.ReferenceProperty(Account)
  target = db.ReferenceProperty(Account)
  type = db.IntegerProperty()

I want to get the equivalence of following query:

SELECT target 
FROM Relationship
WHERE owner = :ke开发者_运维技巧y AND type = :type
ORDERBY target.last_login DESC 

How to do that?

reference: http://www.mail-archive.com/google-appengine@googlegroups.com/msg15878.html


There's no equivalent for that query in datastore. Some points:

  1. You can't select a single property. SELECT is always SELECT * (you select a whole entity).
  2. You can't do joins. You need to denormalize your models to fit the queries you will perform, or perform multiple queries.

So to achieve your goal, you need to have last_login stored in Relationship, or have a 3rd model to serve as index for that specific query.

0

精彩评论

暂无评论...
验证码 换一张
取 消