← Back to Django Course | Chapter 12: Signals & Middleware | Lesson 3 of 8

pre_save नामक Signal

pre_save बिल्कुल सेव होने से पहले bell बजाता है, ताकि आप इसे आखिरी second में adjust कर सकें।
Syntax
markup
from django.db.models.signals import pre_save
from django.dispatch import receiver

@receiver(pre_save, sender=ModelName)
def handler_name(sender, instance, **kwargs):
    instance.field = instance.field.lower()

Save से पहले क्या चलता है?

pre_save Django के database में एक model instance लिखने से ठीक पहले fire होता है, आपको इसे पहले inspect या adjust करने देते हुए।

उदाहरण: What Runs Before a Save?

pre_save fires right before Django writes a model instance to the database, letting you inspect or adjust it first.

markup
from django.db.models.signals import pre_save
from django.dispatch import receiver
from .models import Article

@receiver(pre_save, sender=Article)
def before_article_save(sender, instance, **kwargs):
    print(f"About to save: {instance.title}")
{# 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. #}

एक Field Auto-Normalize करना

pre_save का एक common उपयोग एक field की value को अपने आप साफ़ करना है, जैसे एक email address को store होने से पहले lowercase करना।

उदाहरण: Auto-Normalizing a Field

A common use of pre_save is cleaning up a field's value automatically, such as lowercasing an email address before it's stored.

markup
from django.db.models.signals import pre_save
from django.dispatch import receiver
from .models import Subscriber

@receiver(pre_save, sender=Subscriber)
def normalize_email(sender, instance, **kwargs):
    instance.email = instance.email.strip().lower()
{# 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. #}

नए बनाम मौजूदा Objects Detect करना

pre_save में कोई created flag नहीं है क्योंकि row अभी तक लिखी नहीं गई है। यह जांचने के लिए instance._state.adding जांचें कि यह save एक INSERT होगा या UPDATE।

उदाहरण: Detecting New vs Existing Objects

pre_save has no created flag because the row hasn't been written yet. Check instance._state.adding to tell whether this save will INSERT or UPDATE.

markup
from django.db.models.signals import pre_save
from django.dispatch import receiver
from .models import Article

@receiver(pre_save, sender=Article)
def set_created_flag(sender, instance, **kwargs):
    if instance._state.adding:
        print("This is a brand new Article")
{# 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. #}

pre_save को Fast रखना

pre_save हर save() call पर synchronously चलता है, इसलिए इसके अंदर heavy database queries या external calls से बचें — logic को simple और fast रखें।

उदाहरण: Keeping pre_save Fast

pre_save runs synchronously on every save() call, so avoid heavy database queries or external calls inside it — keep the logic simple and fast.

markup
from django.db.models.signals import pre_save
from django.dispatch import receiver
from .models import Article

@receiver(pre_save, sender=Article)
def guard_title_length(sender, instance, **kwargs):
    if len(instance.title) > 200:
        instance.title = instance.title[:200]
{# 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. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. pre_save में एक नए object पर instance.pk पढ़ने की कोशिश करना और मान लेना कि यह database में पहले से मौजूद है।
  2. pre_save के अंदर expensive queries करना जो हर single save() call को slow कर देता है।
  3. pre_save को एक form के clean() method के साथ confuse करना — pre_save हर save() पर चलता है, सिर्फ form submissions पर नहीं।
चैप्टर सारांश
  • pre_save एक model instance को database में लिखे जाने से बिल्कुल पहले fire होता है।
  • यह commonly field values को save करने से पहले normalize या auto-generate करने के लिए उपयोग होता है।
  • post_save के विपरीत, कोई created flag नहीं है — इसके बजाय instance._state.adding जांचें।
  • pre_save receivers को fast रखें क्योंकि वे हर save() पर synchronously चलते हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.