Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 284525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:27:57+00:00 2026-05-12T05:27:57+00:00

I have to insert 8000+ records into a SQLite database using Django’s ORM. This

  • 0

I have to insert 8000+ records into a SQLite database using Django’s ORM. This operation needs to be run as a cronjob about once per minute.
At the moment I’m using a for loop to iterate through all the items and then insert them one by one.
Example:

for item in items:
    entry = Entry(a1=item.a1, a2=item.a2)
    entry.save()

What is an efficient way of doing this?

Edit: A little comparison between the two insertion methods.

Without commit_manually decorator (11245 records):

nox@noxdevel marinetraffic]$ time python manage.py insrec             

real    1m50.288s
user    0m6.710s
sys     0m23.445s

Using commit_manually decorator (11245 records):

[nox@noxdevel marinetraffic]$ time python manage.py insrec                

real    0m18.464s
user    0m5.433s
sys     0m10.163s

Note: The test script also does some other operations besides inserting into the database (downloads a ZIP file, extracts an XML file from the ZIP archive, parses the XML file) so the time needed for execution does not necessarily represent the time needed to insert the records.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:27:57+00:00Added an answer on May 12, 2026 at 5:27 am

    You want to check out django.db.transaction.commit_manually.

    http://docs.djangoproject.com/en/dev/topics/db/transactions/#django-db-transaction-commit-manually

    So it would be something like:

    from django.db import transaction
    
    @transaction.commit_manually
    def viewfunc(request):
        ...
        for item in items:
            entry = Entry(a1=item.a1, a2=item.a2)
            entry.save()
        transaction.commit()
    

    Which will only commit once, instead at each save().

    In django 1.3 context managers were introduced.
    So now you can use transaction.commit_on_success() in a similar way:

    from django.db import transaction
    
    def viewfunc(request):
        ...
        with transaction.commit_on_success():
            for item in items:
                entry = Entry(a1=item.a1, a2=item.a2)
                entry.save()
    

    In django 1.4, bulk_create was added, allowing you to create lists of your model objects and then commit them all at once.

    NOTE the save method will not be called when using bulk create.

    >>> Entry.objects.bulk_create([
    ...     Entry(headline="Django 1.0 Released"),
    ...     Entry(headline="Django 1.1 Announced"),
    ...     Entry(headline="Breaking: Django is awesome")
    ... ])
    

    In django 1.6, transaction.atomic was introduced, intended to replace now legacy functions commit_on_success and commit_manually.

    from the django documentation on atomic:

    atomic is usable both as a decorator:

    from django.db import transaction
    
    @transaction.atomic
    def viewfunc(request):
        # This code executes inside a transaction.
        do_stuff()
    

    and as a context manager:

    from django.db import transaction
    
    def viewfunc(request):
        # This code executes in autocommit mode (Django's default).
        do_stuff()
    
        with transaction.atomic():
            # This code executes inside a transaction.
            do_more_stuff()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I insert over 8000 bytes into SQL using C#? I have a
I have started learning django and have a problem with INSERT-query using ORM (DB
I have an insert query that gets generated like this INSERT INTO InvoiceDetail (LegacyId,InvoiceId,DetailTypeId,Fee,FeeTax,Investigatorid,SalespersonId,CreateDate,CreatedById,IsChargeBack,Expense,RepoAgentId,PayeeName,ExpensePaymentId,AdjustDetailId)
I have insert 14.485 lines on MySQL like this: INSERT INTO `bairros` (`id`,`cidade_id`,`descricao`) VALUES
If I have an insert statement such as: INSERT INTO MyTable ( Name, Address,
I have to insert roles for user by using check box but for inserting
EDIT: Let me turn this into a straight SQL question... I have a varbinary(max)
I have an INSERT INTO which works fine with the parameters as constants: INSERT
While using sql 2005, it appears I am unable to insert more than 8000
I have just started using RadControls so this question might be basic for you,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.