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

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.

主站蜘蛛池模板: 吐鲁番市| 南澳县| 高碑店市| 庆阳市| 克什克腾旗| 洛南县| 侯马市| 舞钢市| 织金县| 乐至县| 同仁县| 南和县| 铁力市| 滁州市| 大安市| 若羌县| 柘荣县| 电白县| 日照市| 枣庄市| 日照市| 婺源县| 山西省| 美姑县| 青州市| 广平县| 巫山县| 陈巴尔虎旗| 彭水| 慈利县| 东方市| 东莞市| 海晏县| 卢氏县| 共和县| 保德县| 休宁县| 遂平县| 剑河县| 崇礼县| 厦门市|