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

สิทธิ์และการจำกัดความถี่

ควบคุมว่าใครเรียก API ได้และเรียกได้บ่อยเพียงใด

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

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Who Can Call Your API?

An open API lets anyone read or change your data. Permissions decide who is allowed to do what on each endpoint.

The permission_classes Attribute

You attach access rules with permission_classes on a view or ViewSet. DRF checks them before any action runs.

from rest_framework.permissions import IsAuthenticated

class BookViewSet(viewsets.ModelViewSet):
    permission_classes = [IsAuthenticated]

Built-in Permission Classes

DRF ships ready rules: AllowAny opens a view, IsAuthenticated requires a logged-in user, and IsAdminUser limits it to staff.

Read for All, Write for Members

IsAuthenticatedOrReadOnly is a popular middle ground: anyone can GET, but only logged-in users can POST, PUT, or DELETE.

Setting a Default Globally

Set a project-wide rule in settings.py so every view starts secure unless it opts out.

REST_FRAMEWORK = {
    'DEFAULT_PERMISSION_CLASSES': [
        'rest_framework.permissions.IsAuthenticated',
    ],
}

Writing a Custom Permission

Need your own rule? Subclass BasePermission and return True or False from has_permission for the request.

from rest_framework.permissions import BasePermission

class IsOwner(BasePermission):
    def has_object_permission(self, request, view, obj):
        return obj.owner == request.user

Object-Level Permissions

Use has_object_permission to check a single record, like letting users edit only the posts they own. 🔒

Now, Throttling

Permissions answer who. Throttling answers how often, capping the number of requests a client may send in a time window.

Anon vs User Throttles

DRF offers AnonRateThrottle for unauthenticated visitors and UserRateThrottle for logged-in accounts, so you can be stricter with strangers.

Configuring Rate Limits

Set the limits in settings with DEFAULT_THROTTLE_RATES, using a count plus a period like second, minute, hour, or day.

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_RATES': {
        'anon': '20/hour',
        'user': '1000/day',
    },
}

What Throttling Protects

Throttling shields you from abuse and runaway scripts. When a client goes over, DRF returns a 429 Too Many Requests response. 🛡️

Quick Check

Let us confirm the difference between the two gatekeepers.

Recap: Access and Rate Limits

You can now gate endpoints with permission classes and protect them with throttle rates. Together they keep your API both safe and stable. 🎉

คำถามที่พบบ่อย

บทเรียน “สิทธิ์และการจำกัดความถี่” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “สิทธิ์และการจำกัดความถี่” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Django Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Django Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “สิทธิ์และการจำกัดความถี่”

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

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

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

บทเรียน “สิทธิ์และการจำกัดความถี่” ใช้เวลานานแค่ไหน

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

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

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

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

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