Model hooks for Django
Project description
django-model-hooks
Model hooks for Django.
Rationale
Add persistent receivers with logging for django model signals.
Support
Supports: Python 3.10.
Supports Django Versions: 4.2.7
Installation
$ pip install django-model-hooks
Usage
Add model_hooks
to INSTALLED_APPS
.
Run migrations:
python manage.py migrate
Add a module model_hooks_registry
to one or more of your apps. Decorate any function you want to be triggered by
a hook with the model_hook
decorator.
Post serialized data to a rest endpoint on creation of a myapp.MyModel
instance:
# model_hooks_registry.py
import json
import requests
from model_hooks.decorators import model_hook
from model_hooks.events import create_event
from myapp.models import MyModel
from myapp.serializers import serializer
@model_hook(MyModel, create_event, serializer=serializer)
def hook_create(instance, payload, **kwargs):
requests.post(kwargs.get('rest_url'), data=json.dumps(payload))
Or specify a special tag for the triggering of the function call:
# model_hooks_registry.py
import json
import requests
from model_hooks.decorators import model_hook
from model_hooks.events import create_event
from myapp.models import MyModel
from myapp.serializers import serializer
@model_hook(MyModel, create_event, serializer=serializer, tag='rest')
def hook_create(instance, payload, **kwargs):
requests.post(kwargs.get('rest_url'), data=json.dumps(payload))
You may also define a custom event:
# model_hooks_registry.py
import json
import requests
from model_hooks.decorators import model_hook
from model_hooks.events import HookEvent
from myapp.models import MyModel
from myapp.serializers import serializer
custom_event = HookEvent('custom_event')
@model_hook(MyModel, custom_event, serializer=serializer)
def hook_custom(instance, payload, **kwargs):
requests.post(kwargs.get('rest_url'), data=json.dumps(payload))
Trigger the event:
from model_hooks.signals import hook_event
from myapp.model_hooks_registry import custom_event
from myapp.models import MyModel
instance = MyModel.objects.get(pk=1)
hook_event.send(MyModel, model=MyModel, instance=instance, event=custom_event)
Now go to the Django admin and add the hooks for the appropriate models and events.
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-model-hooks-1.0.0.tar.gz
.
File metadata
- Download URL: django-model-hooks-1.0.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f3821b425b4b35c66440528bc3e168e28cb62d0c357f824ffa3f331fd964c8f |
|
MD5 | d248058f2b776eb4f6eff025b4dd1972 |
|
BLAKE2b-256 | a026793651befac6e906c5029a7d00688181419f99ef283c05bddc0d79586cd8 |
Provenance
File details
Details for the file django_model_hooks-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: django_model_hooks-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63176ccb19ba48b49090dfa6fba9476bae502effa24845f3671cf48db5afc694 |
|
MD5 | ec757f38a1f011ae833cceb3efcc16a9 |
|
BLAKE2b-256 | aecf55826c78a8f67677b5a9dfde0d789b45e478c013d4117a2d5fc49eddec6e |