Upgrade to Pro — share decks privately, control downloads, hide ads and more …

[DJUGL] Signals and AppConfig

Ana Balica
October 06, 2015

[DJUGL] Signals and AppConfig

Django User Group in London meetup and my talk about "Signals and AppConfig" Rick and Morty style!

Ana Balica

October 06, 2015
Tweet

More Decks by Ana Balica

Other Decks in Programming

Transcript

  1. from django.db.models.signals import post_save from django.dispatch import receiver from myapp.models

    import MyModel @receiver(post_save, sender=MyModel) def my_handler(sender, **kwargs): pass # my_app/signals.py
  2. from django.db.models.signals import pre_save from django.dispatch import receiver from myapp.models

    import MyModel @receiver(pre_save, sender=MyModel) def my_handler(sender, **kwargs): pass # my_app/signals.py
  3. Django<1.7? make sure that the module it’s in gets imported

    early on so that the signal handling gets registered before any signals need to be sent
  4. Django<1.7? make sure that the module it’s in gets imported

    early on so that the signal handling gets registered before any signals need to be sent
  5. INSTALLED_APPS = [ "my_app.apps.RickNMortyConfig", # ... ] # settings.py or

    default_app_config = "my_app.apps.RickNMortyConfig" # my_app/__init__.py
  6. from django.apps import AppConfig class RickNMortyConfig(AppConfig): name = "rick_n_morty" verbose_name

    = "Rick and Morty” def ready(self): from my_app import signals # my_app/apps.py
  7. • Imports each item from INSTALLED_APPS • Imports the models

    submodule if exists • Runs the ready() method of each app config