验证 - clean 和 full_clean(续)
✅ 实际示例
# ✅ 适合放在 Model.clean() 中 class Course(BaseModel): start_date = models.DateField() end_date = models.DateField() min_students = models.IntegerField() max_students = models.IntegerField() def clean(self): if self.start_date >= self.end_date: raise ValidationError("结束日期不能早于开始日期") if self.min_students > self.max_students: raise ValidationError("最小学生数不能大于最大学生数") # ✅ 应该放在 Service 中 def course_create(...): # 检查讲师是否有资格教授该课程(需要查询关系) if not instructor.qualifications.filter(subject=course.subject).exists(): raise ValidationError("讲师没有资格教授该科目") # 检查时间冲突(复杂查询) if Course.objects.filter( instructor=instructor, start_date__lte=end_date, end_date__gte=start_date ).exists(): raise ValidationError("讲师在此时间段已有其他课程")