part — 11

Signals In Django

from django.db import models
from django.db.models import signals
def create_cookie(sender, instance, created, **kwargs):
print "Let's start making cookies now"
class Cookie(models.Model):
quantity= models.IntegerField()
additions= models.TextField()
signals.post_save.connect(receiver=create_cookie, sender=Cookie)
from django.db import models
from django.db.models import signals
from django.dispatch import receiver

class Cookie(models.Model):
quantity= models.IntegerField()
additions= models.TextField()
@receiver(signals.post_save, sender=Cookie) #sender will be class
def create_cookie(sender, instance, created, **kwargs):
print "Let's start making cookies now"
>>> obj = Cookie(quantity=3, addition='Add Extra choco chips')>>> obj.save()
Cooking is called
from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile
# A post_save signal received when a User instance is created
@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_profile(sender, instance, **kwargs):
instance.profile.save()
from django.apps import AppConfigclass UsersConfig(AppConfig):
name = 'users'
def ready(self):
import users.signals #noqa
default_app_config = ‘users.apps.UsersConfig'

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store