How to query datastore when using ReferenceProperty?

微笑、不失礼 提交于 2019-12-04 15:46:33

The snippet you posted is doing this (see comments):

q = User.all() # prepare User table for querying
q.filter("userEmail =", "az@example.com")  # apply filter, email lookup 
                                              - this is a simple where clause
results = q.fetch(1) # execute the query, apply limit 1
the_user = results[0] # the results is a list of objects, grab the first one

After this code the_user will be an object that corresponds to the user record with email "az@example.com". Seing you've set up your reference properties, you can access its comments and venues with the_user.comments and the_user.venues. Some venue of these can be modified, say like this:

some_venue = the_user.venues[0] # the first from the list
some_venue.venue = 'At DC. square'
db.put(some_venue) # the entry will be updated

I suggest that you make a general sweep of the gae documentation that has very good examples, you will find it very helpful: http://code.google.com/appengine/docs/python/overview.html

** UPDATE **: For adding new venue to user, simply create new venue and assign the queried user object as the venue's user attribute:

new_venue = Venue(venue='Jeferson memorial', user=the_user) # careful with the quoting
db.put(new_venue)

To get all Comments for a given user, filter the user property using the key of the user:

comments = Comment.all().filter("user =", user.key()).fetch(50)

So you could first lookup the user by the email, and then search comments or venues using its key.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!