Frage

I am curious how Django handles model relationships at the object level because I am working on building a custom json serializer, and I need to understand this so I have properly handle nested serialization. I am almost positive I will have to dive into some of the internals of python, but that will not be too big of a deal.

War es hilfreich?

Lösung

The field name in the model has _id appended to it in the table, and it stores the PK of the foreign model (as a FK normally would).

When the related field is accessed on a model, Django performs a query to retrieve the foreign model from the database.

When a model is assigned to the related field, Django reads the PK of the model and assigns it to the backing field in the table.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top