pytoolbox.django.signals.handlers module

apps.py

from django import apps
from django.utils.translation import gettext_lazy as _

from . import signals

__all__ = ('MyApp', )


class MyAppConfig(apps.AppConfig):
    name = 'myapp'
    verbose_name = _('My Application')

    def ready(self):
        signals.connect(self)

signals.py

from django.db.models import signals as dj_signals
from django.db.backends import signals as dj_db_signals
from pytoolbox.django.signals import (
    create_site, setup_postgresql_hstore_extension, strip_strings_and_validate_model
)

# ...

def connect(config):
    '''Connect signal handlers to signals.'''
    dj_db_signals.connection_created.connect(setup_postgresql_hstore_extension)
    dj_signals.post_migrate.connect(create_site, sender=config)
    dj_signals.pre_save.connect(
        strip_strings_and_validate_model, sender=settings.AUTH_USER_MODEL)
pytoolbox.django.signals.handlers.clean_files_delete_handler(instance, signal, **kwargs)[source]

Remove the files of the instance’s file fields when it is removed from the database.

Simply use post_delete.connect(clean_files_delete_handler, sender=<your_model_class>)

Warning

This function remove the file without worrying about any other instance using this file !

Note

Project django-cleanup is a more complete alternative.

pytoolbox.django.signals.handlers.create_site(sender, **kwargs)[source]

Ensure the site name and domain is well configured.

Some alternative:

  • Loading an initial fixture with the values for the site
  • The application django-defaultsite
  • Other options discussed here:
    here
pytoolbox.django.signals.handlers.setup_postgresql_hstore_extension(sender, connection, **kwargs)[source]
pytoolbox.django.signals.handlers.strip_strings_and_validate_model(sender, instance, raw, **kwargs)[source]

Strip the string fields of the instance and run the instance’s full_clean().