开发者

More efficient way to save multiple dependent objects in Django

开发者 https://www.devze.com 2023-01-28 15:34 出处:网络
One a single view, I have to create 3 objects which are dependent one from the other. My guess is that doing 3 database queries is extremely inefficient. Is there a better way of doing it?

One a single view, I have to create 3 objects which are dependent one from the other. My guess is that doing 3 database queries is extremely inefficient. Is there a better way of doing it?

character = char(user = request.user, race = form.clean_race())
character.save()

colony = colony(char = character)
colony.save开发者_开发百科()

buildings = colony_building(colony = colony, building_5 = 1)
buildings.save()

`


Assuming those are different models, you will need one SQL query per model, because SQL's INSERT can only target one table. So doing 3 queries for 3 models is OK to me.

0

精彩评论

暂无评论...
验证码 换一张
取 消