Skip to main content

Adds missing features to Django ORM for working with data in bulk operations

Project description

# Django BulkModel

BulkModel adds additional features that makes it easier to interact with data in bulk.

Below is a brief summary of the project. **Read the [full documentation](https://django-bulkmodel.readthedocs.io/)**

---


## BulkModels and features

Create a bulkmodel by inheriting from `BulkModel`:

from bulkmodel.models import BulkModel

class Foo(BulkModel):
name = models.CharField(max_length=50, blank=False)
value = models.IntegerField(null=False)


Here's some new functionality that's available to you now:

### 1) Get a queryset after a bulk create

Sometimes you need created data to be returned from the database
with a primary key assigned to each model instance for full processing.

This is typically the case when you bulk create some data and then
want to assign foreign keys to other models, thereby requiring the primary keys
on the new data.


from random import randint, random, string

ls = []
for i in range(10):
ls.append(Foo(
# random string
name = ''.join(random.choices(string.ascii_uppercase, 25)),

# random value
value = randint(0, 1000),
))

# create instances and return a queryset of the created items
foos = Foo.objects.bulk_create(ls, return_queryset=True)


### 2) Heterogeneous updates

Django's `update()` queryset method allows you to update **homogeneous data**.

That is, all the model instances in the queryset are updated to the same value (or applied
the same change when using F expressions) for the specified columns.

BulkModels allow you to update data **heterogeneously**. That is,
each model instance in a queryset can take on a different value in a single query execution.

To do this, use the `update_fields()` queryset method, like so:

for foo in foos:
foo.value += randint(100, 200)

# update all fields that changed
foos.update_fields()

# or update just the value field
foos.update_fields('value')

This will only execute one query, regardless of how many fields are in the model are updated.


### 3) Concurrent writes

BulkModels support concurrent writes out of the box, which can significantly
speed up write time given a large enough database server.

Concurrent writes are available on all the write queryset methods
(`bulk_create`, `update`, `update_fields`, `copy_from_objects`).

foos = ...

# concurrently write foos into the database
Foo.objects.bulk_create(foos, concurrent=True, batch_size=1000, max_concurrent_workers=10)

# a regular (homogeneous) update can be written concurrently
foos.update(concurrent=True, batch_size=1000, max_concurrent_workers=10)

# and so can a heterogeneous update
foos.update_fields(concurrent=True, batch_size=1000, max_concurrent_workers=10)


### 4) Additional signals

Django ships with the following signals for interacting with data:

- Saving a single instance: `pre_save` and `post_save`
- Deleting data: `pre_delete` and `post_delete`
- Changing a many to many relationship: `m2m_changed`

What's missing from this list are signals when data is created in bulk and updated in bulk.

BulkModels ship with additional signals when data is created:


- `pre_bulk_create` / `post_bulk_create` are fired when data is being written from `bulk_create`
- `pre_copy_from_instances` / `post_copy_from_instances` are fired when data is being written from a data buffer (via the `copy_from_objects` queryset method)


And these signals when data is updated:

- `pre_update` / `post_update` are fired when a homogeneous update is applied
- `pre_update_fields` / `post_update_fields` are fired when a heterogeneous update is applied


You can optionally turn off signal emission in any write function by setting `send_signals=False`
(signals are emitted by default).

### 5) More

A few more features come with BulkModels, like offline connection management,
copying data to and from the database via in-memory buffers, and queryset chunking.

[See the full list of features](https://django-bulkmodel.readthedocs.io/en/latest/pages/overview-all-features.html)

---

## Installation

First make sure you have django >= 1.9 installed. It's always
recommended to update to the latest version of Django.

For concurrency features to work you'll need Python 3.4+ and access to asyncio

Then install the package from pypi:

pip install django-bulkmodel

Add `bulkmodel` to your `INSTALLED_APPS`:

INSTALLED_APPS = [
...
'bulkmodel',
]


Inherit your existing models from `BulkModel`, or create new models to inherit
from this class:

from django.db import models
from bulkmodel.models import BulkModel

class MyModel(BulkModel):
...


Make migrations:

./manage.py makemigrations <name-of-your-app> # for new apps
./manage.py makemigrations # for existing apps with migrations

And apply them:

./manage.py migrate


You can also do a partial installation if you don't want to migrate all your models.

For more instructions [read the full installation instructions documentation](https://django-bulkmodel.readthedocs.io/en/latest/pages/installation.html)



---

## Full documentation

[Read the full documentation](https://django-bulkmodel.readthedocs.io/)

## License

This software is made available under the Apache v2 License; see the LICENSE file for details.

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-bulkmodel-0.3.0.tar.gz (2.5 MB view details)

Uploaded Source

Built Distribution

django_bulkmodel-0.3.0-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file django-bulkmodel-0.3.0.tar.gz.

File metadata

  • Download URL: django-bulkmodel-0.3.0.tar.gz
  • Upload date:
  • Size: 2.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.6

File hashes

Hashes for django-bulkmodel-0.3.0.tar.gz
Algorithm Hash digest
SHA256 00d5d158a6d1525317de14e1200ecad668d1e1f92f4ed092090709de0b28e286
MD5 330dd0b4d86da3a5f5042c7990efb405
BLAKE2b-256 ee6b3c1bb699b8610816a9c3fee0a5c88224462e3fea34970ee51bf980f87eb3

See more details on using hashes here.

File details

Details for the file django_bulkmodel-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_bulkmodel-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1f61cbc609d74a71e2f89ca08f95473fe2ff86c0c7563a29909abe00a73c903
MD5 69e0f163b774bc9f884800297a3ac15e
BLAKE2b-256 223a2d280c25b5ed5425b4e8e3fbc5e0f2cf2bb2ba12fd2aa87784432b75cff9

See more details on using hashes here.

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