关于在Django中批量保存对象的问题 [英] Question about batch save objects in Django

查看:148
本文介绍了关于在Django中批量保存对象的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django的在线文档中没有看到任何关于这个话题的内容。



我正在尝试将对象列表保存到数据库,但我可以做的是循环遍历列表,并在每个对象上调用save()。



Django数据库是否几次?或者Django会做一个批量保存呢?

解决方案

不幸的是,批量插入是Django 1.3和以前不直接支持的东西。如果要使用ORM,那么您必须在每个对象上调用save()。如果它是一个很大的列表,并且性能是一个问题,您可以使用django.db.cursor在事务中手动插入项来大大加快进程的速度。如果您有一个巨大的数据集,您需要开始查看数据库引擎特定的方法,例如Postgres中的COPY FROM。


I haven't seen any thing on this topic in Django's online documents.

I am trying to save a list of objects to database, but what I can do is loop through the list and call save() on every object.

So does Django hit database several times? Or Django will do one batch save instead?

解决方案

Unfortunately, batch inserts are something that Django 1.3 and prior do not directly support. If you want to use the ORM, then you do have to call save() on each individual object. If it's a large list and performance is an issue, you can use django.db.cursor to INSERT the items manually inside a transaction to dramatically speed the process up. If you have a huge dataset, you need to start looking at Database engine specific methods, like COPY FROM in Postgres.

这篇关于关于在Django中批量保存对象的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆