Django And MySQL: Model Saving Order Is Important
Nov. 19, 2021 0 comments
Often in one Django view you need to save multiple models that have "parent-child" relationship. For example, your view may receive data from a traditional HTML form that includes an inline formset or a JSON with a nested array of child objects from a JavaScript frontend app. In this case you need to save a parent model and create/update/delete some child models. Of course, to make the whole operation atomic all model saves should be wrapped in a database transaction to ensure the consistency of saved data. Let's assume we are using MySQL or MariaDB as a database backend for our Django project. And the question arises: "in which order we should save our models?". "What should we save first: parent or children?" (...)
Featured Posts
-
Running Multiple Celery Beat Instances in One Python Project
Feb. 1, 2021 -
Setting Up MySQL in LibreELEC on Raspberry Pi
Nov. 17, 2017 -
Autodocumenting your Python code with Sphinx - part 2
Feb. 24, 2016