MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Search & Query Parameters0%
Study Materials
REST API Development with DjangoProgress 0%
1.1REST Principles1.2HTTP Methods Overview1.3Status Codes
5.1Filtering API results5.2Search & Query Parameters5.3Pagination Techniques
HomeTutorialsREST API Development with DjangoSearch & Query Parameters
Pagination Techniques

Search & Query Parameters

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

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:

1# api/views.py
2from rest_framework.viewsets import ModelViewSet
3from rest_framework.filters import SearchFilter, OrderingFilter
4from django_filters.rest_framework import DjangoFilterBackend
5from .models import Article
6from .serializers import ArticleSerializer
7 
8class ArticleViewSet(ModelViewSet):
9 queryset = Article.objects.select_related("author").all()
10 serializer_class = ArticleSerializer
11
12 # Enable all three complementary backends
13 filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter]
14
15 # 1. Exact field filtering
16 filterset_fields = ["status", "category"]
17
18 # 2. Text search fields
19 search_fields = ["title", "content", "author__username", "=slug"]
20
21 # 3. Dynamic sorting fields
22 ordering_fields = ["created_at", "view_count", "title"]
23 ordering = ["-created_at"] # Default fallback ordering

2. Advanced Search Prefixes

DRF's SearchFilter supports regex and lookup prefixes in search_fields:

PrefixLookup SyntaxMeaningExample
(None)icontainsCase-insensitive partial match"title" matches "django" anywhere in title
^istartswithCase-insensitive prefix match"^title" matches words starting with term
=iexactCase-insensitive exact match"=slug" matches exact slug string only
@Full-textPostgreSQL full-text search index"@content" (PostgreSQL only)
$RegexCase-insensitive regular expression"$title" matches regex pattern
1search_fields = ["^title", "=slug", "author__email"]

3. Client URL Usage

Clients use the ?search= and ?ordering= query parameters:

Multi-column Text Search:

1# Searches for 'microservice' in title, content, or author username
2GET /api/v1/articles/?search=microservice

Dynamic Column Sorting:

1# Sort ascending by view_count:
2GET /api/v1/articles/?ordering=view_count
3 
4# Sort descending by view_count (using minus sign '-'):
5GET /api/v1/articles/?ordering=-view_count
6 
7# Multi-column sorting (views descending, then title ascending):
8GET /api/v1/articles/?ordering=-view_count,title

4. Customizing Query Parameter Names

If your API convention requires ?q= instead of ?search= or ?sort= instead of ?ordering=:

1# config/settings.py
2REST_FRAMEWORK = {
3 "SEARCH_PARAM": "q",
4 "ORDERING_PARAM": "sort",
5}

Now requests can use /api/v1/articles/?q=docker&sort=-created_at.


Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What query parameter does DRF's SearchFilter inspect by default?
Question 2
What search prefix enforces an exact case-insensitive match (=slug) in search_fields?
Question 3
How does a client specify descending sort order using DRF's OrderingFilter?
Question 4
Why is it critical to explicitly define ordering_fields on your ViewSet?
Question 5
How do you sort by multiple columns simultaneously with OrderingFilter?
Next Lesson

Pagination Techniques

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Filtering API resultsPagination Techniques

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master REST API Development with Django with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext