Skip to content

Instantly share code, notes, and snippets.

@AshishPandagre
Last active October 20, 2020 16:19
Show Gist options
  • Save AshishPandagre/cda58fd513864f0dbe9603e9859a6eaf to your computer and use it in GitHub Desktop.
Save AshishPandagre/cda58fd513864f0dbe9603e9859a6eaf to your computer and use it in GitHub Desktop.
Using signals in django
# some_app/__init__.py
# some_app is the name of the app.
default_app_config = 'some_app.apps.'
# some_app/apps.py
class SomeApp(AppConfig):
name = 'some_app'
def ready(self):
import some_app.mysignal
# create mysignal.py in some_app , this is where the code for our signal is present.
# some_app/mysignal.py
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
from some_app.models import SomeModel
# Here, I implemented a basic signal of save_profile, it gets called once the User is successfully added to the User table.
# For more signals, check out the documentation.
# There are different signals present in Django packages like django-allauth, registration-redux..etc..etc.
# If u are using one of these packages, check out their signals as well, maybe you'll find something useful.
@receiver(post_save, sender=User)
def save_profile(sender, instance, created, **kwargs):
if created:
SomeModel.objects.create(user=instance, name=instance.username)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment