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

Custom Model के Managers

एक custom manager आपके model को उसका अपना personal assistant देने जैसा है जो आपके लिए specific groups of objects fetch करना पहले से जानता है, जैसे 'सिर्फ published वाले'।
Syntax
markup
class CustomManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(field=value)

class ModelName(models.Model):
    objects = models.Manager()
    custom = CustomManager()

Custom Manager क्यों उपयोग करें?

एक custom manager आपको एक common filter, जैसे 'सिर्फ published tutorials', को एक reusable method में package करने देता है बजाय हर जगह filter() repeat करने के।

उदाहरण: Why Use a Custom Manager?

This manager's get_queryset() always starts from only published rows, before any further filtering is applied.

markup
from django.db import models

class PublishedManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(is_published=True)
{# 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. #}

एक Manager को Model से Attach करना

आप एक custom manager को model पर एक class attribute की तरह assign करके attach करते हैं, बिल्कुल एक field की तरह।

Note: दूसरा manager जोड़ना default objects manager को नहीं हटाता जब तक आप इसे पूरी तरह छोड़ न दें।

उदाहरण: Attaching a Manager to a Model

Tutorial.objects.all() still returns everything, while Tutorial.published.all() returns only published tutorials.

markup
class Tutorial(models.Model):
    title = models.CharField(max_length=200)
    is_published = models.BooleanField(default=False)

    objects = models.Manager()
    published = PublishedManager()
{# 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. #}

Custom Manager उपयोग करना

एक बार attach होने पर, custom manager exactly default objects manager जैसे उपयोग होता है, सिर्फ एक अलग नाम के साथ।

उदाहरण: Using the Custom Manager

Both calls automatically start from only published tutorials, since that filter lives inside the manager itself.

markup
from core.models import Tutorial

Tutorial.published.all()
Tutorial.published.filter(category='Forms')
{# 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. default objects manager को इसे वापस जोड़े बिना पूरी तरह override करना, जो Django admin और उस code को तोड़ता है जो Model.objects के मौजूद होने की उम्मीद करता है।
  2. query logic को model के methods के अंदर रखना बजाय एक Manager के, इसे model class से directly unusable बनाते हुए।
  3. एक custom get_queryset() override के अंदर super().get_queryset() call करना भूल जाना, base queryset behavior खोते हुए।
चैप्टर सारांश
  • एक custom Manager एक class है जो models.Manager को subclass करती है और extra query methods define करती है।
  • इसे model पर एक class attribute की तरह attach करें, आमतौर पर objects या किसी ज़्यादा specific नाम से।
  • get_queryset() को override करें ताकि उस manager से हर query के लिए base queryset जो return करती है वह बदल जाए।

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.