bugl
bugl
HomeLearnPatternsSearch
HomeLearnPatternsSearch

Loading lesson path

Learn/Django/QuerySets
Django•QuerySets

Django QuerySet - Filter

Concept visual

Django QuerySet - Filter

Pointer walk
two pointers
leftright102132436485116
left=0
right=6
1
3

Start at both ends

QuerySet Filter

The filter()

method is used to filter your search, and allows you to return only the rows that matches the search term.

As we learned in the previous chapter, we can filter on field names like this:

Example

Return only the records where the firstname is 'Emil':

Formula

mydata = Member.objects.filter(firstname ='Emil').values()

In SQL, the above statement would be written like this:

SELECT * FROM members WHERE firstname = 'Emil';

And

The filter()

method takes the arguments as **kwargs (keyword arguments), so you can filter on more than one field by separating them by a comma.

Example

Return records where lastname is "Refsnes" and id is 2:

Formula

mydata = Member.objects.filter(lastname ='Refsnes', id = 2).values()

In SQL, the above statement would be written like this:

SELECT * FROM members WHERE lastname = 'Refsnes' AND id = 2;

OR To return records where firstname is Emil or firstname is Tobias (meaning: returning records that matches either query, not necessarily both) is not as easy as the AND example above.

We can use multiple filter()

methods, separated by a pipe | character. The results will merge into one model.

Example

Return records where firstname is either "Emil" or Tobias":

Formula

mydata = Member.objects.filter(firstname ='Emil').values() | Member.objects.filter(firstname ='Tobias').values()

Another common method is to import and use Q expressions:

Example

Return records where firstname is either "Emil" or Tobias": from django.http import HttpResponse from django.template import loader from .models import Member from django.db.models import Q

def testing(request):

Formula

mydata = Member.objects.filter(Q(firstname ='Emil') | Q(firstname ='Tobias')).values()
template = loader.get_template('template.html')
context = {
'mymembers': mydata,
}
return HttpResponse(template.render(context, request))

In SQL, the above statement would be written like this:

SELECT * FROM members WHERE firstname = 'Emil' OR firstname = 'Tobias';

Field Lookups

Django has its own way of specifying SQL statements and WHERE clauses. To make specific where clauses in Django, use "Field lookups". Field lookups are keywords that represents specific SQL keywords.

Example:

Use the

__startswith keyword:.filter(firstname__startswith='L');

Is the same as the SQL statement:

WHERE firstname LIKE 'L%'

The above statement will return records where firstname starts with 'L'.

Field Lookups Syntax

All Field lookup keywords must be specified with the fieldname, followed by two(!) underscore characters, and the keyword.

In our

Member model, the statement would be written like this:

Example

Return the records where firstname starts with the letter 'L':

Formula

mydata = Member.objects.filter(firstname__startswith ='L').values()

Field Lookups Reference

A list of all field look up keywords:

Keyword

Description contains

Contains the phrase icontains

Formula

Same as contains, but case - insensitive date

Matches a date day

Formula

Matches a date (day of month, 1 - 31) (for dates)

endswith

Ends with iendswith

Formula

Same as endswidth, but case - insensitive exact

An exact match iexact

Formula

Same as exact, but case - insensitive in

Matches one of the values isnull

Previous

Django QuerySet - Get Data

Next

Django QuerySet - Order By