Combine bulk add, update, and delete into a single call.
Project description
django-bulk-sync
Combine bulk add, update, and delete into a single call.
django-bulk-sync is a package to support the Django ORM, which synthesizes the concepts of bulk_create, bulk_update, and delete into a single call to bulk_sync
.
An example:
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:
from django.db.models import Q
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.
Installation and Quick Start
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
And use as in the example above.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-bulk-sync-1.1.1.tar.gz
.
File metadata
- Download URL: django-bulk-sync-1.1.1.tar.gz
- Upload date:
- Size: 4.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ebe7168ab984b4cc89daeb275935773175eeca65e0f7f488ee70dc1ddb43997 |
|
MD5 | cc271a9f8132f3197973c46c0eca4290 |
|
BLAKE2b-256 | 098e2e2cd64da21d464493dfcec14667c06360b649dc72902bc25117117562ea |
Provenance
File details
Details for the file django_bulk_sync-1.1.1-py3-none-any.whl
.
File metadata
- Download URL: django_bulk_sync-1.1.1-py3-none-any.whl
- Upload date:
- Size: 6.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.7.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00bc9c5be1536c7c220a9815adbcc00e90e71522334d163071174f3ed72a814f |
|
MD5 | f09539cb5ca0e497d53edf0fd7db36d6 |
|
BLAKE2b-256 | ca810b5ff988b63bfe528920f8efad9998555bc4cfcbe04de2f9d2afc5fe457b |