QuerySets में Field Lookups
In this page:
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 के रूप में लिखते हैं।
उदाहरण: What Is a Field Lookup?
Both queries find languages, but iexact ignores letter case while the plain filter needs an exact match.
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 करने देते हैं।
उदाहरण: Comparison Lookups
order__gt=5 returns chapters with an order strictly greater than 5, while gte/lte together define an inclusive range.
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 करते हुए।
उदाहरण: Text Lookups: contains and icontains
The first query only matches titles with Django capitalized exactly that way; the second matches any capitalization.
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 किया हुआ।
उदाहरण: The in Lookup
This returns every tutorial whose category matches one of the three listed values, avoiding three separate OR conditions.
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. #}
- filter() के अंदर exact या gte जैसे lookup के बजाय == उपयोग करना — वहां Python operators काम नहीं करते, सिर्फ keyword lookups करते हैं।
- यह भूल जाना कि lookups default रूप से case-sensitive हैं (exact, contains) और icontains का सहारा सिर्फ तब लेना जब एक search silently कुछ return न करे।
- दो 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 बनाने देते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: