# 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
```shell
$ pip install django-model-hooks
```
## Usage
Add `model_hooks` to `INSTALLED_APPS`.
Run migrations:
```shell
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:
```python
# 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:
```python
# 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:
```python
# 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:
```python
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.
Prune the log table:
```shell
python manage.py log_prune
```
Raw data
{
"_id": null,
"home_page": "https://gitlab.com/luggagecare/django-model-hooks",
"name": "django-model-hooks",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "Django Model hooks",
"author": "Bram Boogaard",
"author_email": "bram@luggo.nl",
"download_url": "https://files.pythonhosted.org/packages/94/54/6bf80a23d895939dfea94abbc19641f4ece375c4cede3443354a578ec0e1/django-model-hooks-1.0.1.tar.gz",
"platform": null,
"description": "# django-model-hooks\n\nModel hooks for Django.\n\n## Rationale\n\nAdd persistent receivers with logging for django model signals.\n\n## Support\n\nSupports: Python 3.10.\n\nSupports Django Versions: 4.2.7\n\n## Installation\n\n```shell\n$ pip install django-model-hooks\n```\n\n## Usage\n\nAdd `model_hooks` to `INSTALLED_APPS`.\n\nRun migrations:\n\n```shell\npython manage.py migrate\n```\n\nAdd a module `model_hooks_registry` to one or more of your apps. Decorate any function you want to be triggered by\na hook with the `model_hook` decorator.\n\nPost serialized data to a rest endpoint on creation of a `myapp.MyModel` instance:\n\n```python\n# model_hooks_registry.py\nimport json\n\nimport requests\nfrom model_hooks.decorators import model_hook\nfrom model_hooks.events import create_event\n\nfrom myapp.models import MyModel\nfrom myapp.serializers import serializer\n\n\n@model_hook(MyModel, create_event, serializer=serializer)\ndef hook_create(instance, payload, **kwargs):\n requests.post(kwargs.get('rest_url'), data=json.dumps(payload))\n```\n\nOr specify a special tag for the triggering of the function call:\n\n```python\n# model_hooks_registry.py\nimport json\n\nimport requests\nfrom model_hooks.decorators import model_hook\nfrom model_hooks.events import create_event\n\nfrom myapp.models import MyModel\nfrom myapp.serializers import serializer\n\n\n@model_hook(MyModel, create_event, serializer=serializer, tag='rest')\ndef hook_create(instance, payload, **kwargs):\n requests.post(kwargs.get('rest_url'), data=json.dumps(payload))\n```\n\nYou may also define a custom event:\n\n```python\n# model_hooks_registry.py\nimport json\n\nimport requests\nfrom model_hooks.decorators import model_hook\nfrom model_hooks.events import HookEvent\n\nfrom myapp.models import MyModel\nfrom myapp.serializers import serializer\n\n\ncustom_event = HookEvent('custom_event')\n\n\n@model_hook(MyModel, custom_event, serializer=serializer)\ndef hook_custom(instance, payload, **kwargs):\n requests.post(kwargs.get('rest_url'), data=json.dumps(payload))\n```\n\nTrigger the event:\n\n```python\nfrom model_hooks.signals import hook_event\n\nfrom myapp.model_hooks_registry import custom_event\nfrom myapp.models import MyModel\n\ninstance = MyModel.objects.get(pk=1)\nhook_event.send(MyModel, model=MyModel, instance=instance, event=custom_event)\n```\n\nNow go to the Django admin and add the hooks for the appropriate models and events.\n\nPrune the log table:\n\n```shell\npython manage.py log_prune\n```\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": "Model hooks for Django",
"version": "1.0.1",
"project_urls": {
"Homepage": "https://gitlab.com/luggagecare/django-model-hooks"
},
"split_keywords": [
"django",
"model",
"hooks"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2bb40950fe3d5b47be093d0f4530417c134907cf0d367c2cf3b3dd640041c05b",
"md5": "29d0b0e2530e2b24dbd340496bcbcd87",
"sha256": "a0c72a1433ab9c9a1d1335c2831a54dc39fface873baa45392d0a3325451b66c"
},
"downloads": -1,
"filename": "django_model_hooks-1.0.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "29d0b0e2530e2b24dbd340496bcbcd87",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 8920,
"upload_time": "2023-12-12T10:41:54",
"upload_time_iso_8601": "2023-12-12T10:41:54.303077Z",
"url": "https://files.pythonhosted.org/packages/2b/b4/0950fe3d5b47be093d0f4530417c134907cf0d367c2cf3b3dd640041c05b/django_model_hooks-1.0.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "94546bf80a23d895939dfea94abbc19641f4ece375c4cede3443354a578ec0e1",
"md5": "763ebe8dd5933ade37d1dd370a65f856",
"sha256": "ffa49b80adca4e0a87d919cd951dfa194f51c5e3bd311898a2e790965dc15801"
},
"downloads": -1,
"filename": "django-model-hooks-1.0.1.tar.gz",
"has_sig": false,
"md5_digest": "763ebe8dd5933ade37d1dd370a65f856",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 6237,
"upload_time": "2023-12-12T10:42:01",
"upload_time_iso_8601": "2023-12-12T10:42:01.891517Z",
"url": "https://files.pythonhosted.org/packages/94/54/6bf80a23d895939dfea94abbc19641f4ece375c4cede3443354a578ec0e1/django-model-hooks-1.0.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-12-12 10:42:01",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "luggagecare",
"gitlab_project": "django-model-hooks",
"lcname": "django-model-hooks"
}