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

QuerySet की Chaining

Querysets को chain करना एक search में step by step filters जोड़ने जैसा है, जहां हर नया filter results को थोड़ा और narrow कर देता है।
Syntax
markup
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.

markup
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 करना।

Note: यह laziness आपको run करने से पहले complex queries efficiently build करने देती है।

उदाहरण: Lazy Evaluation

The database is only hit when the for loop actually iterates over qs, not when it was built.

markup
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.

markup
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. #}
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. यह सोचना कि हर chained call तुरंत एक separate database query चलाता है, जब वे असल में एक query में combine होते हैं जो सिर्फ evaluate होने पर चलती है।
  2. chained calls में वही variable name फिर से assign करना और यह track खोना कि कौन से filters अब भी active हैं।
  3. 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 होते हैं।

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.