Engine数据存储模型参考另一个类

Engine数据存储模型参考另一个类

本文介绍了Google App Engine数据存储模型参考另一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了能够理解数据模型,我基本上都有城市,在每个城市中我会有类别,然后在每个类别中我都有列表。这是我目前所拥有的。

  from google.appengine.ext import db 

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



所以接下来,我想添加:

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

但是,我是否需要指定只有类别应该在类别或类似的情况下才能生效?

您的 City 中的类别属性,并使用 ReferenceProperty 您的类别 class:

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

这也会自动完成为城市模型添加类别集合。


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?

解决方案

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.

这篇关于Google App Engine数据存储模型参考另一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:44