← Back to Django Course | Chapter 4: Models & Django ORM Basics | Lesson 10 of 10

Model का __str__ Method

__str__ method आपके object के लिए एक name tag लिखने जैसा है, ताकि जब Django object print करे तो एक confusing code के बजाय कुछ readable दिखाए।
Syntax
markup
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.

markup
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.

markup
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.

bash
book = Book.objects.get(id=1)
print(book)

⚠️ Run this in your own terminal or Node.js environment.

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. __str__ को undefined छोड़ना, इसलिए हर object admin और shell में एक unhelpful 'Book object (1)' के रूप में दिखता है।
  2. __str__ से एक non-string value return करना, जैसे एक integer, जो एक TypeError raise करता है।
  3. __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 से बना हुआ।

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.