Model की Meta Options
In this page:
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.
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 बन जाती है।
उदाहरण: Setting a Default Ordering
Now Tutorial.objects.all() automatically returns rows sorted by order, without needing order_by(order) every time.
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.
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. #}
- यह भूल जाना कि Meta model की एक inner class होनी चाहिए, एक separate top-level class नहीं।
- Meta में ordering set करना और यह उम्मीद करना कि यह एक explicit order_by() call को override कर दे — जब दोनों मौजूद हों तो order_by() हमेशा जीतता है।
- 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 करते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: