bibi45

2020-07-16 | Backend 中文

DRF 校验:从 is_valid 到可读的错误响应

bibi45

把格式校验、跨字段规则和错误返回放在清晰的边界中。

This article is currently available in Chinese. The language switch changes the site interface; the original text is shown below.

写入数据的固定起点是 serializer.is_valid(raise_exception=True)。它会让 DRF 生成一致的 400 响应,避免每个视图重复处理错误。

def validate(self, attrs):
    if attrs["start_at"] >= attrs["end_at"]:
        raise serializers.ValidationError("start_at must be before end_at")
    return attrs

单字段规则使用字段参数或 validate_字段名;跨字段规则用 validate。数据库唯一约束和并发约束仍需在持久化层兜底。

© 2026 bibi45. All rights reserved.

Built as a static, Git-owned site.