Django基于多个字段(文件字段)验证表单
django 有没有办法根据多个字段验证表单。我见过一些例子,人们建议重写表单的 clean 方法,并在无法满足自定义验证时引发 ValidationError 。对我来说,问题是我不确定您是否可以检查文件是否是从 clean 方法中上传的。我只能使用请求对象来访问它们,而您无权在表单的 clean 方法中访问请求对象。
Is there a way in django to validate a form based on multiple fields. I have seen some examples where people recommend overriding a form's clean method and raising a ValidationError if it fails to meet your custom validation. The problem for me is that I'm not sure you can check whether or not a file was uploaded from within the clean method. I have only been able to access them using the request objects and you do not have access to the request object within the form's clean method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您所描述的方法(从
Form.clean
中引发ValidationError
)是执行 多字段验证。您可以在
clean
方法中从self.files
访问上传的文件。来自 django/forms/forms.py :The method you've described (raising
ValidationError
from withinForm.clean
) is the official way to do multi-field validation.You can access uploaded files from
self.files
within theclean
method. Fromdjango/forms/forms.py
: