Model का __str__ Method
class ModelName(models.Model):
field_name = models.CharField(max_length=100)
def __str__(self):
return f'{self.field_name}'
__str__ क्यों मायने रखता है
__str__ method के बिना, एक model instance print करने पर एक unhelpful default दिखता है जैसे 'Book object (1)', जो debugging और admin browsing को harder बनाता है।
उदाहरण: Why __str__ Matters
Without a __str__ method, printing a model instance shows an unhelpful default like 'Book object (1)', which makes debugging and admin browsing harder.
class Book(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.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. #}
एक Readable String बनाना
__str__ एक f-string के साथ कई fields combine करके सिर्फ एक field से ज़्यादा descriptive label produce कर सकता है।
उदाहरण: Building a Readable String
__str__ can combine multiple fields with an f-string to produce a more descriptive label than just one field.
class Order(models.Model):
customer_name = models.CharField(max_length=100)
total = models.IntegerField()
def __str__(self):
return f'{self.customer_name} - {self.total}'
{# 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. #}
इसे Action में देखना
एक बार __str__ define हो जाए, shell में एक instance print करना या एक QuerySet पर loop करना generic object reference के बजाय readable label दिखाता है।
उदाहरण: Seeing It in Action
Once __str__ is defined, printing an instance or looping over a QuerySet in the shell shows the readable label instead of a generic object reference.
book = Book.objects.get(id=1)
print(book)
⚠️ Run this in your own terminal or Node.js environment.
- __str__ को undefined छोड़ना, इसलिए हर object admin और shell में एक unhelpful 'Book object (1)' के रूप में दिखता है।
- __str__ से एक non-string value return करना, जैसे एक integer, जो एक TypeError raise करता है।
- __str__ से expensive database queries करवाना, जहां भी object print या list होता है उसे धीमा करते हुए, जैसे admin site।
- __str__ एक method है जो object को represent करने वाला एक human-readable string return करता है।
- Django __str__ को admin site, shell, और templates में objects display करने के लिए उपयोग करता है।
- __str__ के बिना, objects एक generic 'ModelName object (id)' के रूप में दिखते हैं।
- __str__ को एक string return करना चाहिए, object की खुद की fields से बना हुआ।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: