Blog Archive, November 2021

Featured Image

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?" (...)

Read post

 DjangoMariaDBMySQLPython