0Pricing
Django Academy · บทเรียน

การกรอง การค้นหา และการแบ่งหน้า

ทำให้การตอบกลับจาก API ขนาดใหญ่ใช้งานได้สะดวก

การกรอง การค้นหา และการแบ่งหน้า เป็นบทเรียน Django Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การกรอง การค้นหา และการแบ่งหน้า”

ทำให้การตอบกลับจาก API ขนาดใหญ่ใช้งานได้สะดวก คุณปฏิบัติ Django Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Django Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Django Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การกรอง การค้นหา และการแบ่งหน้า” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Django Academy นี้ได้ไหม

ได้ บทเรียน Django Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ModelViewSet และเราเตอร์
  2. สิทธิ์และการจำกัดความถี่
  3. การยืนยันตัวตนด้วยโทเค็นและ JWT
  4. การกรอง การค้นหา และการแบ่งหน้า
← กลับไปที่ Django Academy