pre_save नामक Signal
In this page:
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.
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.
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.
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.
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. #}
- pre_save में एक नए object पर instance.pk पढ़ने की कोशिश करना और मान लेना कि यह database में पहले से मौजूद है।
- pre_save के अंदर expensive queries करना जो हर single save() call को slow कर देता है।
- 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 चलते हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: