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.
Asked
Active
Viewed 40 times
1 Answers
3
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.

Ignacio Vazquez-Abrams
- 776,304
- 153
- 1,341
- 1,358