开发者

[Google App Engine]How to use filter()?

开发者 https://www.devze.com 2023-02-18 11:43 出处:网络
I\'m getting started with Google App Engine, and I want to make something like twitter. class User(db.Model):

I'm getting started with Google App Engine, and I want to make something like twitter.

class User(db.Model):
  account = db.StringProperty()
  password = db.StringProper开发者_JS百科ty()
  name = db.StringProperty()

class Message(db.Model):
  user = db.ReferenceProperty()
  text = StringProperty()
  created = DateTimeProperty(auto_now=True)

How to get particular person's tweet? I try

que = db.Query(Message).order('-created').filter("user['account'] = ",'Tom')

But this is error. Can I access the user properties?


There is no GQL JOIN operation. Check out the article on modeling relationships for some other techniques you can use. Also, be sure to watch Building Scalable, Complex Apps on App Engine, it will be well worth your time.

With your current models, you would need to fetch the user entity (or build its key!) then query using that. For example:

user = User.all(keys_only=True).filter('account', 'Tom').get()  # only get one.
# or, build the key (if possible)
user = db.Key.from_path('User', 'tom')  # if you make 'account' the key_name

messages = Message.all().order('-created').filter("user =", user)

Another choice is to denomalize the desgin and store 'account' on the message as well:

class Message(db.Model):
  user = db.ReferenceProperty()
  account = StringProperty()
  text = StringProperty()
  created = DateTimeProperty(auto_now=True)

messages = Message.all().order('-created').filter("account =", 'tom')


What Robert Kluin said was right. Also i see this in your model.

user = db.ReferenceProperty()

I think it is supposed to be user=db.ReferenceProperty(User)

0

精彩评论

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

关注公众号