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

การตรวจสอบข้อมูลแบบกำหนดเองด้วยเมธอด clean

เพิ่มกฎการตรวจสอบระดับฟิลด์และฟอร์ม

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

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

Beyond Built-in Rules

Model fields give you basic checks, but real apps need custom rules. Django runs your own clean methods to enforce exactly the logic you want. 🧹

Field-Level clean

To validate one field, add a method named clean_fieldname. Django calls it automatically during validation for that single field.

def clean_title(self):
    title = self.cleaned_data["title"]
    return title

Read from cleaned_data

Inside a clean method, pull the value from cleaned_data. By this point Django has already converted it to the right Python type for you.

title = self.cleaned_data["title"]

Raise to Reject

If a value breaks your rule, raise a ValidationError. Django catches it and shows the message next to that field in the form.

from django.core.exceptions import ValidationError
raise ValidationError("Title is too short.")

A Real Field Check

Here we reject titles shorter than five characters. Note you must return the value at the end so it stays in cleaned_data.

def clean_title(self):
    title = self.cleaned_data["title"]
    if len(title) < 5:
        raise ValidationError("Too short.")
    return title

Always Return the Value

The golden rule of clean_fieldname: return the cleaned value. Forget it and the field becomes None, even when input was valid.

Form-Level clean

To compare two fields, override the whole-form clean method. It runs after every field-level check has finished.

def clean(self):
    cleaned = super().clean()
    return cleaned

Call super().clean() First

Begin your form-level clean by calling super().clean(). That gives you the full cleaned_data dict gathered from the field checks.

cleaned = super().clean()
start = cleaned.get("start")
end = cleaned.get("end")

Compare Two Fields

Now you can enforce cross-field rules, like making sure end comes after start. Raise ValidationError when the relationship is wrong.

if start and end and end < start:
    raise ValidationError("End must be after start.")

Attach Errors to a Field

In form-level clean you can target one field with add_error. The message then appears right beside that input, not at the top.

self.add_error("end", "End must be after start.")

Order of Validation

Django runs each clean_fieldname first, then the form-level clean last. Knowing this order helps you place each rule in the right spot.

Quick Check

Where should a rule that compares two fields live?

Recap: Custom Validation

Well done! Use clean_fieldname for single fields and form-level clean for cross-field rules. Raise ValidationError to reject, and always return cleaned values. 🎉

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

บทเรียน “การตรวจสอบข้อมูลแบบกำหนดเองด้วยเมธอด clean” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การตรวจสอบข้อมูลแบบกำหนดเองด้วยเมธอด clean”

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

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

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

บทเรียน “การตรวจสอบข้อมูลแบบกำหนดเองด้วยเมธอด clean” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การประกาศ ModelForm และ Meta.fields
  2. form.save() และ commit=False
  3. การตรวจสอบข้อมูลแบบกำหนดเองด้วยเมธอด clean
  4. วิดเจ็ต ป้ายกำกับ และข้อความช่วยเหลือ
← กลับไปที่ Django Academy