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

QuerySets में Field Lookups

Field lookups special question words जैसे हैं जो आप एक search में जोड़ते हैं ताकि Django exactly समझे कि आपका मतलब क्या है, जैसे उन names के लिए पूछना जो किसी चीज़ को contain करें या किसी चीज़ से 'greater than' हों।
Syntax
markup
ModelName.objects.filter(field__lookup=value)

# Examples of lookups: exact, iexact, contains, icontains,
# gt, gte, lt, lte, startswith, in

Field Lookup क्या है?

एक field lookup Django को बताता है कि एक field की value कैसे compare करनी है, न कि सिर्फ इसे किस value से compare करना है। आप इसे filter() या exclude() के अंदर fieldname__lookuptype के रूप में लिखते हैं।

Note: बिना lookup suffix के, Django exact matching पर default करता है।

उदाहरण: What Is a Field Lookup?

Both queries find languages, but iexact ignores letter case while the plain filter needs an exact match.

markup
from core.models import Language

# Exact match (default)
Language.objects.filter(slug='python')

# Case-insensitive match
Language.objects.filter(name__iexact='python')
{# 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. #}

Comparison Lookups

gt, gte, lt, और lte आपको numbers और dates को exact equality के बजाय 'greater than' या 'less than' से filter करने देते हैं।

Note: gte और lte combine करें एक range filter करने के लिए, जैसे दो dates के बीच।

उदाहरण: Comparison Lookups

order__gt=5 returns chapters with an order strictly greater than 5, while gte/lte together define an inclusive range.

markup
from core.models import Chapter

# Chapters after order 5
Chapter.objects.filter(order__gt=5)

# Chapters from order 1 to 5
Chapter.objects.filter(order__gte=1, order__lte=5)
{# 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. #}

Text Lookups: contains और icontains

contains check करता है कि कोई text एक field के अंदर कहीं भी appear करता है या नहीं, और icontains वही करता है लेकिन case ignore करते हुए।

Note: User-facing search boxes के लिए icontains उपयोग करें ताकि Django और django दोनों match हों।

उदाहरण: Text Lookups: contains and icontains

The first query only matches titles with Django capitalized exactly that way; the second matches any capitalization.

markup
from core.models import Tutorial

# Case-sensitive substring search
Tutorial.objects.filter(title__contains='Django')

# Case-insensitive substring search
Tutorial.objects.filter(title__icontains='django')
{# 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. #}

in Lookup

in check करता है कि एक field की value कई choices में से एक है या नहीं, Python के in keyword जैसा लेकिन SQL में translate किया हुआ।

Note: in को एक list, tuple, या यहां तक कि एक दूसरी queryset भी pass करें।

उदाहरण: The in Lookup

This returns every tutorial whose category matches one of the three listed values, avoiding three separate OR conditions.

markup
from core.models import Tutorial

# Tutorials in any of these categories
Tutorial.objects.filter(category__in=['Tutorial', 'Forms', 'APIs'])
{# 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. filter() के अंदर exact या gte जैसे lookup के बजाय == उपयोग करना — वहां Python operators काम नहीं करते, सिर्फ keyword lookups करते हैं।
  2. यह भूल जाना कि lookups default रूप से case-sensitive हैं (exact, contains) और icontains का सहारा सिर्फ तब लेना जब एक search silently कुछ return न करे।
  3. दो underscores गलत तरीके से chain करना, जैसे price_gte लिखना बजाय price__gte के, जिसे Django समझता नहीं और एक FieldError raise करता है।
चैप्टर सारांश
  • Field lookups field name के बाद एक double underscore उपयोग करते हैं, जैसे price__gte=100।
  • Common lookups में exact, iexact, contains, icontains, gt, gte, lt, lte, और in शामिल हैं।
  • Lookups आपको raw SQL लिखे बिना precise filters बनाने देते हैं।

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.