Django Signals का परिचय
In this page:
import django.dispatch
signal_name = django.dispatch.Signal()
signal_name.send(sender=SenderClass, key=value)
Django Signal क्या है?
एक signal Django का built-in publish/subscribe system है। एक piece of code एक signal send करता है जब कुछ होता है, और कई receiver functions कहीं और इसे listen कर सकते हैं और react कर सकते हैं, बिना sender को यह जानने की ज़रूरत के कि कौन सुन रहा है।
उदाहरण: What Is a Django Signal?
A signal is Django's built-in publish/subscribe system. One piece of code sends a signal when something happens, and any number of receiver functions elsewhere can listen for it and react, without the sender needing to know who's listening.
import django.dispatch
order_placed = django.dispatch.Signal()
def notify_shipping(sender, order_id, **kwargs):
print(f"Notify shipping: order {order_id} placed")
order_placed.connect(notify_shipping)
order_placed.send(sender=None, order_id=42)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Signals बनाम Direct Function Calls
एक signal के लिए reach करने से पहले, पूछें कि क्या एक plain function call ज़्यादा simple होगा। एक direct call trace और debug करना आसान है; एक signal तब बेहतर है जब sender को genuinely यह नहीं जानना चाहिए कि कौन react करता है।
उदाहरण: Signals vs Direct Function Calls
Before reaching for a signal, ask whether a plain function call would be simpler. A direct call is easier to trace and debug; a signal is better when the sender genuinely shouldn't know who reacts to it.
# Without signals: caller directly imports and calls
from shipping.utils import notify_shipping
def place_order(order_id):
save_order(order_id)
notify_shipping(order_id)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Built-in Signals का Overview
Django model lifecycle events के लिए कई ready-made signals ship करता है, जैसे django.db.models.signals से post_save, pre_save, और post_delete।
उदाहरण: Built-in Signals Overview
Django ships several ready-made signals for model lifecycle events, such as post_save, pre_save, and post_delete from django.db.models.signals.
from django.db.models.signals import post_save, post_delete
def log_save(sender, instance, **kwargs):
print(f"{sender.__name__} saved: {instance.pk}")
post_save.connect(log_save)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
@receiver से एक Receiver Connect करना
@receiver decorator एक function को एक signal से connect करने का सबसे common shorthand है, optionally sender= के साथ एक model तक scoped।
उदाहरण: Connecting a Receiver with @receiver
The @receiver decorator is the most common shorthand for connecting a function to a signal, optionally scoped to one model with sender=.
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.contrib.auth.models import User
@receiver(post_save, sender=User)
def welcome_new_user(sender, instance, created, **kwargs):
if created:
print(f"Welcome, {instance.username}!")
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- यह मानना कि signals कई receivers के across एक guaranteed order में चलते हैं।
- Simple logic के लिए signals का overuse करना जिसे एक plain function call ज़्यादा clearly handle कर देता।
- Signals module import करना भूल जाना इसलिए इसके receivers असल में connect नहीं होते।
- Signals एक part of Django को दूसरे decoupled parts को notify करने देते हैं जब कुछ होता है।
- django.dispatch.Signal आपको fully custom signals define और send करने देता है।
- Receivers बस @receiver या .connect() से एक signal से connected functions हैं।
- Signals cross-app decoupling के लिए convenient हैं लेकिन trace करने के लिए एक extra layer of indirection जोड़ते हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: