QuerySet की Chaining
queryset = (
ModelName.objects
.filter(field=value)
.exclude(other_field=other_value)
.order_by('field')
) # runs SQL only when evaluated
Chaining क्या है?
filter() या order_by() जैसा हर QuerySet method एक और QuerySet return करता है, इसलिए आप इसके ठीक बाद एक और method call कर सकते हैं, एक query को piece by piece बनाते हुए।
उदाहरण: What Is Chaining?
This first narrows results to tutorials in the Tutorial category, then sorts just those results alphabetically.
from core.models import Tutorial
# Chain filter() and order_by() together
Tutorial.objects.filter(category='Tutorial').order_by('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. #}
Lazy Evaluation
आपके methods chain करते समय Django असल में database query नहीं करता — यह तब तक wait करता है जब तक आप कुछ ऐसा न करें जिसे real data चाहिए, जैसे queryset पर loop करना।
उदाहरण: Lazy Evaluation
The database is only hit when the for loop actually iterates over qs, not when it was built.
from core.models import Tutorial
qs = Tutorial.objects.filter(category='Forms') # no query yet
qs = qs.order_by('title') # still no query
for t in qs: # query runs here
print(t.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. #}
filter() और exclude() को Combine करना
filter() matching rows रखता है, जबकि exclude() matching rows हटाता है — उन्हें chain करना आपको एक condition include करने और दूसरी rule out करने देता है।
उदाहरण: Combining filter() and exclude()
The result only includes published tutorials from the Tutorial category — both conditions apply together.
from core.models import Tutorial
# Tutorials in 'Tutorial' category, excluding drafts
Tutorial.objects.filter(category='Tutorial').exclude(is_published=False)
{# 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. #}
- यह सोचना कि हर chained call तुरंत एक separate database query चलाता है, जब वे असल में एक query में combine होते हैं जो सिर्फ evaluate होने पर चलती है।
- chained calls में वही variable name फिर से assign करना और यह track खोना कि कौन से filters अब भी active हैं।
- queryset को [:n] से slice करने के बाद .filter() call करना, जिसे Django allow नहीं करता।
- filter(), exclude(), और order_by() जैसे QuerySet methods नई QuerySets return करते हैं, इसलिए उन्हें chain किया जा सकता है।
- Chaining तब तक database को hit नहीं करती जब तक queryset evaluate न हो, जैसे इस पर loop करके या इसे print करके।
- Chained filters default रूप से AND logic से combine होते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: