2

I am trying to understand the 1-to-many relationships in datastore; but I fail to understand how query and update the record of a user when the model includes ReferenceProperty. Say I have this model:

class User(db.Model):
    userEmail = db.StringProperty()
    userScore = db.IntegerProperty(default=0)

class Comment(db.Model):
    user = db.ReferenceProperty(User, collection_name="comments")
    comment = db.StringProperty()

class Venue(db.Model):
    user = db.ReferenceProperty(User, collection_name="venues")
    venue = db.StringProperty()

If I understand correctly, the same user, uniquely identified by userEmail can have many comments and may be associated with many venues (restaurants etc.)

Now, let's say the user az@example.com is already in the database and he submits a new entry.

Based on this answer I do something like:

q = User.all()
q.filter("userEmail =", az@example.com)
results = q.fetch(1)
newEntry = results[0]

But I am not clear what this does! What I want to do is to update comment and venue fields which are under class Comment and class Venue.

Can you help me understand how this works? Thanks.

Community
  • 1
  • 1
Zeynel
  • 13,145
  • 31
  • 100
  • 145

2 Answers2

4

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)
vonPetrushev
  • 5,457
  • 6
  • 39
  • 51
  • Great explanation, very helpful, thank you. But I still do not understand how I add a new venue associated with the user. In the example you give, I grab a venue which is already in the database and change it. But this user is posting new comment about a new venue. – Zeynel Nov 28 '10 at 00:32
  • Many thanks for your help with this question (especially, your line-by-line comments). – Zeynel Nov 28 '10 at 02:02
1

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.

Karl Rosaen
  • 4,508
  • 2
  • 27
  • 30
  • 1
    Actually, he does not have to do that - once he loads the user with email lookup, the comments are easily accessed with `user.comments`. – vonPetrushev Nov 28 '10 at 00:05
  • Thanks for your answer. I don't know how to find the key to search comments or venues. I have to read what that means. But in this case, what I want to do, is to understand, how to add a comment and a venue to a user. How do I update a user's record with a new comment? – Zeynel Nov 28 '10 at 00:12
  • @vonPetrushev: Thanks again for your answer. How do I "load the user with email lookup?" Not sure what this means. – Zeynel Nov 28 '10 at 00:14