horrible_nerd
horrible_nerd

Reputation: 125

doing too many write operations in django rest framework and postgres

I have this excel sheet where i can have as many as 3000-5000 or may be more data( email ids) and i need to insert each of these into my db one by one .I have been asked not to make these many write operations in one go .How should i go about solving this so that i don't do so many entries to database ?Any hint will be highly appreciated . The solution i could think of is this . https://www.caktusgroup.com/blog/2019/01/09/django-bulk-inserts/

Upvotes: 0

Views: 122

Answers (2)

ahmad dana
ahmad dana

Reputation: 41

I'd vote for doing bulk operations withithe django model itself you can for example do the following with your model class :

... Entry.objects.bulk_create([
...     Entry(headline='This is a test'),
...     Entry(headline='This is only a test'),
... ])

(code from django website)

you can also you transaction dicorator were you can in sure that a database trasaction is atomic (All or Nothing), thisis handy when you want to do long processeing for your entities or transform them

from django.db import transaction

@transaction.atomic

decorate your method that inserts records into the database

Upvotes: 2

Greg
Greg

Reputation: 1991

I think that the link you included would work fairly well!

Just wanted to throw one other option out there. Many databases have batch/bulk upload capabilities. Depending on what DB you're using, you could potentially connect directly to it, and perform your writes without Django.

Upvotes: 0

Related Questions