user1362255
user1362255

Reputation: 35

GAE Datastore ID

I've created two different Entities, one a User and one a Message they can create. I assign each user an ID and then want to assign this ID to each message which that user creates. How can I go about this? Do I have to do it in a query?

Thanks

Upvotes: 1

Views: 86

Answers (1)

Thanos Makris
Thanos Makris

Reputation: 3115

Assuming that you are using Python NDB, you can having something like the following:

class User(ndb.Model):
    # put your fileds here

class Message(ndb.Model):
    owner = ndb.KeyProperty()
    # other fields

Create and save a User:

user = User(field1=value1, ....)
user.put()

Create and save a Message:

message = Message(owner=user.key, ...)
message.put()

Query a message based on user:

messages = Message.query().filter(Message.owner==user.key).fetch() # returns a list of messages that have this owner

For more information about NDB, take a look at Python NDB API.

Also, you should take a look at Python Datastore in order to get a better understanding of data modeling in App Engine.

Upvotes: 2

Related Questions