Skip to main content

Combine bulk add, update, and delete into a single call.

Project description

django-bulk-sync

Combine bulk create, update, and delete into a single call.

django-bulk-sync is a package for the Django ORM that combines bulk_create, bulk_update, and delete into a single method call to bulk_sync.

It manages all necessary creates, updates, and deletes with as few database calls as possible to maximize performance.

Installation

The package is available on pip as [django-bulk-sync][django-bulk-sync]. Run:

pip install django-bulk-sync

then import via:

from bulk_sync import bulk_sync

A Usage Scenario

Companies have zero or more Employees. You want to efficiently sync the names of all employees for a single Company from an import from that company, but some are added, updated, or removed. The simple approach is inefficient -- read the import line by line, and:

For each of N records:

  • SELECT to check for the employee's existence
  • UPDATE if it exists, INSERT if it doesn't

Then figure out some way to identify what was missing and delete it. As is so often the case, the speed of this process is controlled mostly by the number of queries run, and here it is about two queries for every record, and so O(N).

Instead, with bulk_sync, we can avoid the O(N) number of queries, and simplify the logic we have to write as well.

Example Usage

from django.db.models import Q
from bulk_sync import bulk_sync

new_models = []
for line in company_import_file:
	# The `.id` (or `.pk`) field should not be set. Instead, `key_fields` 
	# tells it how to match.
	e = Employee(name=line['name'], phone_number=line['phone_number'], ...)
	new_models.append(e)

# `filters` controls the subset of objects considered when deciding to 
# update or delete.
filters = Q(company_id=501)  
# `key_fields` matches an existing object if all `key_fields` are equal.
key_fields = ('name', )  
ret = bulk_sync(
        new_models=new_models,
        filters=filters,
        key_fields=key_fields)

print("Results of bulk_sync: "
      "{created} created, {updated} updated, {deleted} deleted."
      		.format(**ret['stats']))

Under the hood, it will atomically call bulk_create, bulk_update, and a single queryset delete() call, to correctly and efficiently update all fields of all employees for the filtered Company, using name to match properly.

Argument Reference

def bulk_sync(new_models, key_fields, filters, batch_size=None):

  • new_models: An iterable of Django ORM Model objects that you want stored in the database. They may or may not have id set, but you should not have already called save() on them.
  • key_fields: Identifying attribute name(s) to match up new_models items with database rows. If a foreign key is being used as a key field, be sure to pass the fieldname_id rather than the fieldname.
  • filters: Q() filters specifying the subset of the database to work in.
  • batch_size: passes through to Django bulk_create.batch_size and bulk_update.batch_size, and controls how many objects are created/updated per SQL query. """

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-bulk-sync-1.1.2.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

django_bulk_sync-1.1.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file django-bulk-sync-1.1.2.tar.gz.

File metadata

  • Download URL: django-bulk-sync-1.1.2.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.9.1 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for django-bulk-sync-1.1.2.tar.gz
Algorithm Hash digest
SHA256 13e56d5c1aa154d45da8b7063306b00fcae886191111464dd908c56d263b8043
MD5 4db7ee2d49ff18c4dd14e35b55f0ab8f
BLAKE2b-256 3ce770c75648031d1f6258852ea15b90bb57ebc7fc4b8225289a95ce69ba3049

See more details on using hashes here.

Provenance

File details

Details for the file django_bulk_sync-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: django_bulk_sync-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.9.1 setuptools/40.6.3 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.5

File hashes

Hashes for django_bulk_sync-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9d63a20cca44fbd49f3ba7b1d30ff0cb3ac5216975d3be3ff6e6dfb83f51833a
MD5 de017fa853a84b0671c160e799f990cd
BLAKE2b-256 328292d8d796af19dddbd57e02dae75ade54a604a4d823805b776086f6da0f91

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page