过滤、搜索与分页
让大型 API 响应更易于使用
过滤、搜索与分页 是 CoddyKit 上的免费 Django Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Django Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Django Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Big Responses Are a Problem
Returning thousands of rows in one response is slow and unwieldy. DRF gives you filtering, search, and pagination to keep payloads usable.
Why Pagination First
Pagination splits a large list into pages, so clients fetch a slice at a time instead of everything at once.
Turning On Pagination
Set a default pagination class and page size in settings, and every list view starts paginating automatically.
REST_FRAMEWORK = {
'DEFAULT_PAGINATION_CLASS':
'rest_framework.pagination.PageNumberPagination',
'PAGE_SIZE': 10,
}The Paginated Shape
PageNumberPagination wraps results with count, next, and previous links, so clients can walk through pages easily. 📄
Pagination Styles
Pick a style: PageNumberPagination uses page numbers, while LimitOffsetPagination lets clients ask for a limit and offset directly.
Now, Filtering
Filtering narrows results by exact field values, like asking only for books where the author id is 5.
Adding DjangoFilterBackend
Install django-filter, enable DjangoFilterBackend, and list the fields clients may filter on with filterset_fields.
class BookViewSet(viewsets.ModelViewSet):
queryset = Book.objects.all()
filterset_fields = ['author', 'published_year']Filters as Query Params
Clients filter through the URL query string, so a request like /books/?author=5 returns only that author's books. 🔍
Search vs Filter
SearchFilter does fuzzy text matching across fields, while filtering matches exact values. Search is for free-text queries.
Enabling Search Fields
Add SearchFilter and list the columns to scan in search_fields. Clients then pass a search term with the search query param.
from rest_framework.filters import SearchFilter
class BookViewSet(viewsets.ModelViewSet):
filter_backends = [SearchFilter]
search_fields = ['title', 'author__name']Ordering Results
Add OrderingFilter so clients can sort with ordering=title or ordering=-price for descending order, all from the URL.
Quick Check
Make sure you can tell these tools apart.
Recap: Usable API Responses
You can now slice lists with pagination, narrow them with filters, find text with search, and sort with ordering. Your API stays fast and friendly. 🎉
常见问题解答
「过滤、搜索与分页」课时是免费的吗?
是的 — 「过滤、搜索与分页」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Django Academy 课程的其余内容,请升级到 CoddyKit PRO。 Django Academy 课程共包含 4 节课。
「过滤、搜索与分页」这节课中我会学到什么?
让大型 API 响应更易于使用 你通过在浏览器中直接运行的动手代码来练习 Django Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Django Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Django Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「过滤、搜索与分页」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Django Academy 课中编写并运行代码吗?
能。每节 Django Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- ModelViewSet 与路由器
- 权限与限流
- 令牌与 JWT 认证
- 过滤、搜索与分页