Bulk create model objects in django

后端 未结 8 2038
执笔经年
执笔经年 2020-12-13 05:17

I have a lot of objects to save in database, and so I want to create Model instances with that.

With django, I can create all the models instances, with MyMode

8条回答
  •  渐次进展
    2020-12-13 06:06

    worked for me to use manual transaction handling for the loop(postgres 9.1):

    from django.db import transaction
    with transaction.commit_on_success():
        for item in items:
            MyModel.objects.create(name=item.name)
    

    in fact it's not the same, as 'native' database bulk insert, but it allows you to avoid/descrease transport/orms operations/sql query analyse costs

提交回复
热议问题