Google App Engine Data Store Model Reference Another Class

╄→гoц情女王★ 提交于 2019-12-11 15:10:05

问题


So that you can understand the data model, I basically have cities and within each one I'll have categories and then inside each category I'll have listings. Here's what I have so far.

from google.appengine.ext import db

class City(db.Model):
    name = db.StringProperty(required=True)
    connections = db.ListProperty()
    categories = db.ListProperty()

So Next, I want to add:

class Category(db.Model)
    name = db.StringProperty(required=True)

But do I need to specify that only Category should be in categories or something to that effect?


回答1:


You need to throw the categories property from your City and use a ReferenceProperty in your Category class:

class Category(db.Model)
    name = db.StringProperty(required=True)
    city = db.ReferenceProperty(City, collection_name = 'categories')

This will also automatically add categories collection for your City model.




回答2:


You want to look at a custom property named KeyListProperty in App Engine Patch. That will give you the sort of many-to-many relationship you want.



来源:https://stackoverflow.com/questions/4107660/google-app-engine-data-store-model-reference-another-class

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