简体   繁体   中英

How do I make child and parent entities in GAE Python?

I have the following database models in my application:

class Account(ndb.Model):
    username = ndb.StringProperty()
    userid = ndb.IntegerProperty()
    email = ndb.StringProperty()

class Post(nbd.Model):
    text = nbd.StringProperty()

How can I tell the program that "many posts belong to one account" ?

Thanks!

There are a couple ways I think. One easy way is to just add another field on the Post model which ties it to the Account. eg assuming that all accounts have a unique userid :

def Post(ndb.Model):
    text = ndb.StringProperty()
    userid = ndb.IntegerProperty()

Now, if you have an account, you can get it's "children" by querying where the userid is the same as that of the account.

A second way is to structure your data for "strong consistency" by using "ancestor paths" . Then you can get the posts by doing an "ancestor query" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM