2

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 ?

0

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It's a very interesting possibility but I have multiple interconnected tables so it's not the most optimized solution I can imagine.
You know the primary key of all objects being inserted?
I define some primary keys but there is some tables with linked objects that use the django default serial field as primary key. I think it could be a problem to create an insertion script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.