Custom Model के Managers
In this page:
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.
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 की तरह।
उदाहरण: Attaching a Manager to a Model
Tutorial.objects.all() still returns everything, while Tutorial.published.all() returns only published tutorials.
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.
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. #}
- default objects manager को इसे वापस जोड़े बिना पूरी तरह override करना, जो Django admin और उस code को तोड़ता है जो Model.objects के मौजूद होने की उम्मीद करता है।
- query logic को model के methods के अंदर रखना बजाय एक Manager के, इसे model class से directly unusable बनाते हुए।
- एक 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 करती है वह बदल जाए।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: