- Mastering MongoDB 3.x
- Alex Giamas
- 84字
- 2021-08-20 10:10:53
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.
推薦閱讀
- 嵌入式系統(tǒng)及其開發(fā)應(yīng)用
- 我的J2EE成功之路
- 數(shù)控銑削(加工中心)編程與加工
- Visual C# 2008開發(fā)技術(shù)詳解
- 網(wǎng)絡(luò)綜合布線技術(shù)
- 單片機(jī)技術(shù)一學(xué)就會
- 突破,Objective-C開發(fā)速學(xué)手冊
- 實(shí)用網(wǎng)絡(luò)流量分析技術(shù)
- 嵌入式GUI開發(fā)設(shè)計(jì)
- 空間機(jī)器人智能感知技術(shù)
- 從零開始學(xué)ASP.NET
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)學(xué)習(xí)指導(dǎo)與練習(xí)(Windows XP+Office 2003)
- Mastercam X5應(yīng)用技能基本功特訓(xùn)
- 單片機(jī)硬件接口電路及實(shí)例解析
- 暗戰(zhàn)強(qiáng)人:黑客及反黑客工具快速精通