django Field lookups
Table of contents
Field lookups
exact
Entry.objects.get(id__exact=14) |
Entry.objects.get(id__exact=None) |
iexact
Case-insensitive exact match.
Blog.objects.get(name__iexact=’beatles blog’) |
Blog.objects.get(name__iexact=None) |
contains
Case-sensitive containment test.
Entry.objects.get(headline__contains=’Lennon’) |
icontains
Case-insensitive containment test.
Entry.objects.get(headline__icontains=’Lennon’) |
in
In a given iterable; often a list, tuple, or queryset. It’s not a common use case, but strings (being iterables) are accepted. Examples:
Entry.objects.filter(id__in=[1, 3, 4]) |
Entry.objects.filter(headline__in=’abc’) |
gt
Greater than.
Entry.objects.filter(id__gt=4) |
gte
Greater than or equal to.
lt
Less than.
lte
Less than or equal to.
startswith
Case-sensitive starts-with.
Entry.objects.filter(headline__startswith=’Lennon’) |
istartswith
Case-insensitive starts-with.
Entry.objects.filter(headline__istartswith=’Lennon’) |
endswith
Case-sensitive ends-with.
Entry.objects.filter(headline__endswith=’Lennon’) |
iendswith
Case-insensitive ends-with.
Entry.objects.filter(headline__iendswith=’Lennon’) |
range
import datetime
start_date = datetime.date(2005, 1, 1)
end_date = datetime.date(2005, 3, 31)
Entry.objects.filter(pub_date__range=(start_date, end_date)) |
date
For datetime fields, casts the value as date. Allows chaining additional field lookups. Takes a date value.
Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1)) |
Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1)) |
year
For date and datetime fields, an exact year match. Allows chaining additional field lookups. Takes an integer year.
Entry.objects.filter(pub_date__year=2005) |
Entry.objects.filter(pub_date__year__gte=2005) |
iso_year
New in Django 2.2. For date and datetime fields, an exact ISO 8601 week-numbering year match. Allows chaining additional field lookups. Takes an integer year.
Entry.objects.filter(pub_date__iso_year=2005) |
Entry.objects.filter(pub_date__iso_year__gte=2005) |
month
For date and datetime fields, an exact month match. Allows chaining additional field lookups. Takes an integer 1 (January) through 12 (December).
Entry.objects.filter(pub_date__month=12) |
Entry.objects.filter(pub_date__month__gte=6) |
day
For date and datetime fields, an exact day match. Allows chaining additional field lookups. Takes an integer day.
Entry.objects.filter(pub_date__day=3) |
Entry.objects.filter(pub_date__day__gte=3) |
week
For date and datetime fields, return the week number (1-52 or 53) according to ISO-8601, i.e., weeks start on a Monday and the first week contains the year’s first Thursday.
Entry.objects.filter(pub_date__week=52) |
Entry.objects.filter(pub_date__week__gte=32, pub_date__week__lte=38) |
week_day
For date and datetime fields, a ‘day of the week’ match. Allows chaining additional field lookups. Takes an integer value representing the day of week from 1 (Sunday) to 7 (Saturday).
Entry.objects.filter(pub_date__week_day=2) |
Entry.objects.filter(pub_date__week_day__gte=2) |
quarter
For date and datetime fields, a ‘quarter of the year’ match. Allows chaining additional field lookups. Takes an integer value between 1 and 4 representing the quarter of the year. Example to retrieve entries in the second quarter (April 1 to June 30):
Entry.objects.filter(pub_date__quarter=2) |
time
For datetime fields, casts the value as time. Allows chaining additional field lookups. Takes a datetime.time value.
Entry.objects.filter(pub_date__time=datetime.time(14, 30)) |
Entry.objects.filter(pub_date__time__range=(datetime.time(8), datetime.time(17))) |
hour
For datetime and time fields, an exact hour match. Allows chaining additional field lookups. Takes an integer between 0 and 23.
Event.objects.filter(timestamp__hour=23) |
Event.objects.filter(time__hour=5) |
Event.objects.filter(timestamp__hour__gte=12) |
minute
For datetime and time fields, an exact minute match. Allows chaining additional field lookups. Takes an integer between 0 and 59.
Event.objects.filter(timestamp__minute=29) |
Event.objects.filter(time__minute=46) |
Event.objects.filter(timestamp__minute__gte=29) |
second
For datetime and time fields, an exact second match. Allows chaining additional field lookups. Takes an integer between 0 and 59.
Event.objects.filter(timestamp__second=31) |
Event.objects.filter(time__second=2) |
Event.objects.filter(timestamp__second__gte=31) |
isnull
Takes either True or False, which correspond to SQL queries of IS NULL and IS NOT NULL, respectively.
Entry.objects.filter(pub_date__isnull=True) |
regex
Case-sensitive regular expression match. The regular expression syntax is that of the database backend in use. In the case of SQLite, which has no built in regular expression support, this feature is provided by a (Python) user-defined REGEXP function, and the regular expression syntax is therefore that of Python’s re module.
Entry.objects.get(title__regex=r'^(An?|The) +') |
iregex
Case-insensitive regular expression match.
Entry.objects.get(title__iregex=r'^(an?|the) +') |