官术网_书友最值得收藏!

Inheritance with PyMODM models

Handling one-one and one-many relationships in MongoDB can be done using references or embedding. This example shows both ways: references for the model user and embedding for the comment model:

from pymodm import EmbeddedMongoModel, MongoModel, fields

class Comment(EmbeddedMongoModel):
author = fields.ReferenceField(User)
content = fields.CharField()

class Post(MongoModel):
title = fields.CharField()
author = fields.ReferenceField(User)
revised_on = fields.DateTimeField()
content = fields.CharField()
comments = fields.EmbeddedDocumentListField(Comment)

Similar to Mongoid for Ruby, we can define relationships as being embedded or referenced, depending on our design decision.

主站蜘蛛池模板: 扶绥县| 衡南县| 黑河市| 长泰县| 石柱| 陵水| 临沂市| 龙游县| 岳普湖县| 东阿县| 土默特左旗| 建湖县| 三都| 舒城县| 浪卡子县| 缙云县| 山丹县| 曲阳县| 萝北县| 阿拉尔市| 辽阳市| 威海市| 镇平县| 遂溪县| 千阳县| 香港| 广昌县| 涞水县| 鸡东县| 广灵县| 区。| 哈密市| 香格里拉县| 咸阳市| 黄大仙区| 临沧市| 景洪市| 永安市| 专栏| 青冈县| 巴楚县|