mongodbtornadomotorengine

MotorEngine - How to represent the equivalent of the foreign key in model?


In MongoDB I have a document that represents a Balance which has an Stakeholder's ID as a field.

I need to relate these two classes, Balance and Stakeholder, but I don't know what's the proper way. I've seen there's a field that could be appropiate but I still don't understand it: EmbeddedDocumentField()

class Balance(Document):
    id = UUIDField()
    creation_date = DateTimeField(auto_now_on_insert=True)
    gross_balance = FloatField(required=True, min_value=0, default=0)
    balances_description = StringField(required=True, max_length=255)
    stake_holder = #FK to Stakeholder

class Stakeholder(Document):
    ...

Any idea?


Solution

  • If Stakeholder represents document from other collection and stake_holder is ObjectId, you should use ReferenceField()

    stake_holder = ReferenceField(reference_document_type=Stakeholder)