问题
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