Bulk create model objects in django

后端 未结 8 1897
执笔经年
执笔经年 2020-12-13 05:17

I have a lot of objects to save in database, and so I want to create Model instances with that.

With django, I can create all the models instances, with MyMode

相关标签:
8条回答
  • 2020-12-13 05:54

    Use bulk_create() method. It's standard in Django now.

    Example:

    Entry.objects.bulk_create([
        Entry(headline="Django 1.0 Released"),
        Entry(headline="Django 1.1 Announced"),
        Entry(headline="Breaking: Django is awesome")
    ])
    
    0 讨论(0)
  • 2020-12-13 05:59

    The easiest way is to use the create Manager method, which creates and saves the object in a single step.

    for item in items:
        MyModel.objects.create(name=item.name)
    
    0 讨论(0)
  • as of the django development, there exists bulk_create as an object manager method which takes as input an array of objects created using the class constructor. check out django docs

    0 讨论(0)
  • 2020-12-13 06:03

    Check out this blog post on the bulkops module.

    On my django 1.3 app, I have experienced significant speedup.

    0 讨论(0)
  • 2020-12-13 06:06

    worked for me to use manual transaction handling for the loop(postgres 9.1):

    from django.db import transaction
    with transaction.commit_on_success():
        for item in items:
            MyModel.objects.create(name=item.name)
    

    in fact it's not the same, as 'native' database bulk insert, but it allows you to avoid/descrease transport/orms operations/sql query analyse costs

    0 讨论(0)
  • 2020-12-13 06:07

    Here is how to bulk-create entities from column-separated file, leaving aside all unquoting and un-escaping routines:

    SomeModel(Model):
        @classmethod
        def from_file(model, file_obj, headers, delimiter):
            model.objects.bulk_create([
                model(**dict(zip(headers, line.split(delimiter))))
                for line in file_obj],
                batch_size=None)
    
    0 讨论(0)
提交回复
热议问题