Skip to main content

This package adds stagers to assist in bulk operations on django models.

Project description

Django Stagers

This package provides a Stager class to stage objects for bulk creation, update, or deletion at a later time.

When syncing data from an external API or data source, this package helps by handling which objects in the database are new, existing, or need updating. That way, you can focus on the sync logic without tracking model instances or their unique identifiers.

Installation

Install using pip:

pip install django-stagers

Import the Stager class:

from django_stagers import Stager

Usage

Instantiate a Stager class (optionally with a type for better type-hinting) and then call the create() function to add models. When any of the three staging methods create(), delete() or update() are called, though, the corresponding action is not actually taken in the database until the user calls commit(), at which point all of the staged actions are run in bulk fashion.

from django_stagers import Stager

foo_stager = Stager[Foo](queryset=Foo.objects.all(), key="id")

foo_stager.create(Foo(attr="abc"))
foo_stager.create(Foo(attr="def"))
...

foo_stager.commit()

The output of the operation will look something like this:

Sat Nov 2 2024 2:36:37 PM [INFO    ]: Committing staged Foo instances.
Sat Nov 2 2024 2:36:37 PM [INFO    ]: Created     37 Foo instances.

Existing

Another key feature of stagers is that they keep track of existing model instances in the database using the existing attribute. This way, it is easy to check whether a new object is already tracked in the django database or has already been staged previously.

for serialized_foo in serialized_foos:
  if existing_foo := foo_stager.existing.get(serialized_foo['key']):
    foo_stager.update(serialized_foo['key'], 'attr', serialized_foo['attr'])
  else:
    foo_stager.create(Foo(**serialized_foo))
...

foo_stager.commit()

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_stagers-0.0.5.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_stagers-0.0.5-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file django_stagers-0.0.5.tar.gz.

File metadata

  • Download URL: django_stagers-0.0.5.tar.gz
  • Upload date:
  • Size: 3.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for django_stagers-0.0.5.tar.gz
Algorithm Hash digest
SHA256 85037fc3ad2bdb5278a0e62f260ed9f78fddb8a5daee82c16919d6bd2679c589
MD5 979cc8889911be2bef22c5928c5769f3
BLAKE2b-256 0cbaae338ec1291541c9054cf21c62d4bd75ddd267dba56b783c2c5d96e2e320

See more details on using hashes here.

File details

Details for the file django_stagers-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: django_stagers-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for django_stagers-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 de7cc165d5579942789edfcd41cabeb853ac8377da50ba375be43bcc2f3a1c4a
MD5 0434e682f39191f796b6bd20b1c09480
BLAKE2b-256 49ff3891d899c4172238aeea450982958cca73116c5c62b58f2679fbbd94103e

See more details on using hashes here.

Supported by

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