- 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.
推薦閱讀
- 自動控制工程設(shè)計入門
- 大數(shù)據(jù)技術(shù)基礎(chǔ)
- 網(wǎng)上沖浪
- 圖形圖像處理(Photoshop)
- 返璞歸真:UNIX技術(shù)內(nèi)幕
- 數(shù)據(jù)庫原理與應(yīng)用技術(shù)學(xué)習(xí)指導(dǎo)
- 計算機網(wǎng)絡(luò)應(yīng)用基礎(chǔ)
- Hands-On Linux for Architects
- Data Wrangling with Python
- CompTIA Network+ Certification Guide
- TensorFlow Reinforcement Learning Quick Start Guide
- 網(wǎng)絡(luò)管理工具實用詳解
- ESP8266 Robotics Projects
- Learning Apache Apex
- 生物3D打印:從醫(yī)療輔具制造到細(xì)胞打印