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

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.

主站蜘蛛池模板: 马龙县| 和政县| 涡阳县| 克拉玛依市| 桃园市| 增城市| 东源县| 武清区| 阳山县| 宁海县| 合作市| 大洼县| 昭觉县| 安福县| 马鞍山市| 五台县| 桓仁| 庄浪县| 渝北区| 和政县| 尼木县| 醴陵市| 弋阳县| 苗栗市| 通辽市| 雅江县| 定襄县| 青铜峡市| 轮台县| 玉龙| 莒南县| 嘉禾县| 五寨县| 大洼县| 潜江市| 丰台区| 榆树市| 团风县| 偃师市| 独山县| 津南区|