Thibaut Guirimand
Thibaut Guirimand

Reputation: 911

Create an SQL inserting script from Django to populate a PostgreSQL database

I'm currently trying to insert in a Django model some complex data extracted from multiple parsed files. Because I have a huge amount of data to insert I don't want to multiply the queries to the database from my Django script.

Is there a way to generate an SQL script instead of executing all the object_to_insert.save() and inserting the script by using psql -f my_script.sql ?

Upvotes: 2

Views: 2472

Answers (1)

Jean Jung
Jean Jung

Reputation: 1210

If it's only one table, there is a bulk_create method in the Django ORM queryset: https://docs.djangoproject.com/en/stable/ref/models/querysets/#bulk-create

This method takes a list of objects and insert them on the database by an an efficient way, generally in only one query if the database supports multiple inserts.

Upvotes: 2

Related Questions