我对wagtail.core.models.Page
后代进行了XOR验证:
def save(self, *args, **kwargs):
if self.video_playlist is not None and self.gallery is not None:
raise ValidationError(_("Only gallery or only video playlist must be filled"))
elif self.video_playlist is None and self.gallery is None:
raise ValidationError(_("Only gallery or only video playlist must be filled"))
super(OkoPage, self).save(*args, **kwargs)
答案 0 :(得分:2)
您应该在模型的clean
方法中而不是在save
中执行此验证。这样,ValidationError
将被Django的表单处理逻辑捕获并转换为表单上的错误消息。
save
方法仅在表单处理完成后才调用,因此此时抛出ValidationError
太晚了,无法很好地处理它。