<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Raymond Kipkorir</title>
    <description>The latest articles on DEV Community by Raymond Kipkorir (@raykipkorir).</description>
    <link>https://dev.to/raykipkorir</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F805309%2F1e4c3ab9-fc9a-4efc-8e33-ec08b9e74dd8.jpeg</url>
      <title>DEV Community: Raymond Kipkorir</title>
      <link>https://dev.to/raykipkorir</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/raykipkorir"/>
    <language>en</language>
    <item>
      <title>QUICK INTRO TO DJANGO SIGNALS</title>
      <dc:creator>Raymond Kipkorir</dc:creator>
      <pubDate>Wed, 13 Jul 2022 13:15:48 +0000</pubDate>
      <link>https://dev.to/raykipkorir/quick-intro-to-django-signals-395</link>
      <guid>https://dev.to/raykipkorir/quick-intro-to-django-signals-395</guid>
      <description>&lt;h2&gt;
  
  
  What are Django Signals?
&lt;/h2&gt;

&lt;p&gt;Django signals help separate applications in a project to communicate. In that, when a specific event occurs it triggers the occurrence of another event.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to use signals...&lt;/strong&gt;&lt;br&gt;
Django signals are just like the green light in a traffic light, if the green light comes on it &lt;em&gt;triggers&lt;/em&gt; the movement of cars. &lt;/p&gt;

&lt;p&gt;A common use case of Django signals is when you have a &lt;strong&gt;User Model&lt;/strong&gt; that has a One to One relationship with a &lt;strong&gt;Profile Model&lt;/strong&gt;. Here we aim to create a Profile instance anytime a user object is created or update an existing profile instance when the user object is updated. &lt;em&gt;This is where signals come to the rescue&lt;/em&gt;. Signals listen to the post-event of the User Model that is after the &lt;strong&gt;.save() method&lt;/strong&gt; is called. Once the .save() method is called a &lt;strong&gt;post_save signal&lt;/strong&gt;  is sent, which triggers a &lt;strong&gt;receiver function&lt;/strong&gt; that creates the profile instance or update the existing instance.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's write some code ...&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Before we move on, we've noticed that there are &lt;em&gt;senders&lt;/em&gt; and &lt;em&gt;receivers&lt;/em&gt; in signals...&lt;br&gt;
&lt;strong&gt;Senders&lt;/strong&gt; must either be a Python object or None which will receive signals from any sender&lt;br&gt;
&lt;strong&gt;Receivers&lt;/strong&gt; must either be a function or instance method&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;users/models.py
from django.db import models

class User(models.Model):
   name = models.CharField(max_length = 200)
   email = models.EmailField()

#another application models   
profile/models.py
from django.db import models

class Profile(models.Model):
   user= models.OnetoOneField(User, on_delete = models.CASCADE)
   email = models.EmailField()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our models are all set, so let's use the Django built-in signals specifically the &lt;em&gt;model signals&lt;/em&gt; which is &lt;strong&gt;post_save signal&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;profile/signals.py
from .models import Profile
from base.models import User
from django.dispatch import receiver
from django.db.models.signals import post_save

@receiver(post_save, sender = User)
def create_profile(sender, instance, created, *args, **kwargs):
    if created:
        Profile.objects.create(user  = instance)

#post_save.connect(create_profile, sender = User)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To connect the sender and receiver you can either use the &lt;strong&gt;@receiver decorator&lt;/strong&gt; or the &lt;strong&gt;.connect() method&lt;/strong&gt; of the &lt;strong&gt;Signal&lt;/strong&gt; instance.&lt;/p&gt;

&lt;p&gt;So what will happen here ...&lt;br&gt;
Once a user object is saved a profile instance is created where the user field is populated with the user instance created.&lt;/p&gt;

&lt;p&gt;Our last bit will be &lt;strong&gt;configurations&lt;/strong&gt; of the signals.py file...&lt;/p&gt;

&lt;p&gt;Inside our apps.py we will create a &lt;strong&gt;ready()&lt;/strong&gt; function where we will import our &lt;strong&gt;signals.py&lt;/strong&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;profile/apps.py
from django.apps import AppConfig

class ProfileConfig(AppConfig):
    default_auto_field = "django.db.models.BigAutoField"
    name = "profile"

    def ready(self):
        import profile.signals
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In addition,&lt;br&gt;
In the &lt;em&gt;settings.py&lt;/em&gt; under the &lt;strong&gt;INSTALLED_APPS&lt;/strong&gt; make sure to register your app like this &lt;code&gt;"profile.apps.ProfileConfig"&lt;/code&gt; , if registered by only indicating the apps name i.e &lt;code&gt;"profile"&lt;/code&gt;  make sure to write the following code in &lt;strong&gt;&lt;code&gt;__init__.py&lt;/code&gt;&lt;/strong&gt; file of the package where the signals.py file is located.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;profile/__init__.py
default_app_config = 'profile.apps.ProfileConfig'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check out the &lt;a href="https://docs.djangoproject.com/en/4.0/topics/signals/"&gt;Django docs&lt;/a&gt; to learn more built-in Django signals.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;QUOTE OF TODAY&lt;/strong&gt; by &lt;em&gt;William James&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you can change your mind, you can change your life.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Thanks for reading 😄&lt;/p&gt;

</description>
      <category>django</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
