Search & Query Parameters
DRF SearchFilter and OrderingFilter
While DjangoFilterBackend performs exact and structured field matches, users frequently need a single search input to query multiple text fields simultaneously (e.g. searching across customer name, order ID, and notes), as well as interactive column sorting. DRF includes SearchFilter and OrderingFilter out of the box.
1. Enabling SearchFilter and OrderingFilter
Configure backends on your ViewSet:
2. Advanced Search Prefixes
DRF's SearchFilter supports regex and lookup prefixes in search_fields:
| Prefix | Lookup Syntax | Meaning | Example |
|---|---|---|---|
| (None) | icontains | Case-insensitive partial match | "title" matches "django" anywhere in title |
^ | istartswith | Case-insensitive prefix match | "^title" matches words starting with term |
= | iexact | Case-insensitive exact match | "=slug" matches exact slug string only |
@ | Full-text | PostgreSQL full-text search index | "@content" (PostgreSQL only) |
$ | Regex | Case-insensitive regular expression | "$title" matches regex pattern |
3. Client URL Usage
Clients use the ?search= and ?ordering= query parameters:
Multi-column Text Search:
Dynamic Column Sorting:
4. Customizing Query Parameter Names
If your API convention requires ?q= instead of ?search= or ?sort= instead of ?ordering=:
Now requests can use /api/v1/articles/?q=docker&sort=-created_at.
Practice Quiz
Pagination Techniques
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Filtering API results | Pagination Techniques |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.