Skip to main content

Hook-style hooks for Django bulk operations like bulk_create and bulk_update.

Project description

django-bulk-hooks

⚡ Bulk hooks for Django bulk operations.

django-bulk-hooks brings a declarative, hook-like experience to Django's bulk_create, bulk_update, and bulk_delete — including support for BEFORE_ and AFTER_ hooks, conditions, batching, and transactional safety.

✨ Features

  • Declarative hook system: @hook(AFTER_UPDATE, condition=...)
  • BEFORE/AFTER hooks for create, update, delete
  • Hook-aware manager that wraps Django's bulk_ operations
  • Hook chaining, hook deduplication, and atomicity
  • Class-based hook handlers with DI support

🚀 Quickstart

pip install django-bulk-hooks

Define Your Model

from django.db import models
from django_bulk_hooks.manager import BulkHookManager

class Account(models.Model):
    balance = models.DecimalField(max_digits=10, decimal_places=2)
    objects = BulkHookManager()

Create a Hook Handler

from django_bulk_hooks import hook, AFTER_UPDATE, HookHandler
from django_bulk_hooks.conditions import WhenFieldHasChanged
from .models import Account

class AccountHookHandler(HookHandler):
    @hook(AFTER_UPDATE, model=Account, condition=WhenFieldHasChanged("balance"))
    def log_balance_change(self, new_objs):
        print("Accounts updated:", [a.pk for a in new_objs])

🛠 Supported Hook Events

  • BEFORE_CREATE, AFTER_CREATE
  • BEFORE_UPDATE, AFTER_UPDATE
  • BEFORE_DELETE, AFTER_DELETE

🧠 Why?

Django's bulk_ methods bypass signals and save(). This package fills that gap with:

  • Hooks that behave consistently across creates/updates/deletes
  • Scalable performance via chunking (default 200)
  • Support for @hook decorators and centralized hook classes

📦 Usage in Views / Commands

# Calls AFTER_UPDATE hooks automatically
Account.objects.bulk_update(accounts, ['balance'])

# Triggers BEFORE_CREATE and AFTER_CREATE hooks
Account.objects.bulk_create(accounts)

🧩 Integration with Queryable Properties

You can extend from BulkHookManager to support formula fields or property querying.

class MyManager(BulkHookManager, QueryablePropertiesManager):
    pass

📝 License

MIT © 2024 Augend / Konrad Beck

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

django_bulk_hooks-0.1.67.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

django_bulk_hooks-0.1.67-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file django_bulk_hooks-0.1.67.tar.gz.

File metadata

  • Download URL: django_bulk_hooks-0.1.67.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.4 CPython/3.11.9 Windows/10

File hashes

Hashes for django_bulk_hooks-0.1.67.tar.gz
Algorithm Hash digest
SHA256 6a6722dc07d448701df08355cf68958a6bbf92c2c5a4fb607f5fd30f92f133c4
MD5 0bfd0985beaef1a1bc6c70f119295891
BLAKE2b-256 71f7bd76f04353cbe6ffc27c24425994b89f7bd24a581702435221bd98d94979

See more details on using hashes here.

File details

Details for the file django_bulk_hooks-0.1.67-py3-none-any.whl.

File metadata

File hashes

Hashes for django_bulk_hooks-0.1.67-py3-none-any.whl
Algorithm Hash digest
SHA256 c02ab2ab6ab6df09aefd49d06044210f97992207aae70e5b340e52002a5e9144
MD5 3ded070d260bf57faa62889f57be65ea
BLAKE2b-256 af886a50cba330ef44521bf7593880bb3533c1ff30fea9f30c16cec3d468fab7

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