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.
SearchFilter and OrderingFilterConfigure backends on your ViewSet:
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 |
Clients use the ?search= and ?ordering= query parameters:
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.
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Filtering API results | Pagination Techniques |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.