← Back to Django Course | Chapter 6: Advanced ORM Queries | Lesson 8 of 9

Model की Meta Options

Meta options एक model से जुड़ी एक rulebook जैसी हैं, Django को इसके बारे में extra details बताते हुए, जैसे rows किस order में list करने हैं या admin में इसे क्या कहना है।
Syntax
markup
class ModelName(models.Model):
    field_name = models.CharField(max_length=100)

    class Meta:
        ordering = ['field_name']
        verbose_name_plural = 'plural name'

Meta Class क्या है?

Meta एक छोटी class है जिसे आप एक model के अंदर nest करते हैं ताकि model के बारे में चीज़ें configure कर सकें, इसकी fields से अलग।

उदाहरण: What Is the Meta Class?

The Meta class here tells Django to always sort Chapter querysets by the order field unless order_by() overrides it.

markup
from django.db import models

class Chapter(models.Model):
    title = models.CharField(max_length=200)
    order = models.IntegerField()

    class Meta:
        ordering = ['order']
{# 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. #}

एक Default Ordering Set करना

ordering option field names की एक list है जो उस model पर हर queryset के लिए default sort order बन जाती है।

Note: किसी field को default रूप से descending sort करने के लिए उसके आगे - लगाएं।

उदाहरण: Setting a Default Ordering

Now Tutorial.objects.all() automatically returns rows sorted by order, without needing order_by(order) every time.

markup
class Tutorial(models.Model):
    title = models.CharField(max_length=200)
    order = models.IntegerField()

    class Meta:
        ordering = ['order']
{# 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. #}

unique_together से Uniqueness Enforce करना

unique_together यह सुनिश्चित करता है कि fields का एक combination rows के across कभी repeat न हो, भले ही हर field अलग से repeat हो सके।

उदाहरण: Enforcing Uniqueness with unique_together

This allows the same slug to be reused across different languages, but never twice within the same language.

markup
class Tutorial(models.Model):
    language_id = models.IntegerField()
    slug = models.SlugField()

    class Meta:
        unique_together = ('language_id', 'slug')
{# 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. यह भूल जाना कि Meta model की एक inner class होनी चाहिए, एक separate top-level class नहीं।
  2. Meta में ordering set करना और यह उम्मीद करना कि यह एक explicit order_by() call को override कर दे — जब दोनों मौजूद हों तो order_by() हमेशा जीतता है।
  3. option names को गलत spell करना जैसे orderin बजाय ordering के, जिसे Django कुछ Django versions में unknown Meta attributes के लिए error raise करने के बजाय silently ignore करता है।
चैप्टर सारांश
  • class Meta model-wide behavior configure करने के लिए एक model के अंदर nested class है।
  • Common options में ordering, verbose_name, और unique_together शामिल हैं।
  • Meta options यह बदले बिना automatically apply होते हैं कि आप objects कैसे बनाते या query करते हैं।

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.