我有一个带有图像字段的django ModelForm
。我想在用户提交表单时调整图像大小,但前提是他们上传了新图像。我的代码如下所示:
class EditProfileForm(forms.ModelForm):
class Meta:
model = Person
fields = ['picture', '...']
def clean_picture(self):
picture = self.cleaned_data['picture']
if picture.file: #This isn't right though
#resize it
return picture
但是,如果正在编辑的模型包含文件,则picture.file
似乎始终存在。我知道我可以在视图中查看request.FILES
,但这非常不优雅。还有更好的方法吗?
答案 0 :(得分:1)
通常,您可以使用self.changed_data
执行此操作,该this post返回值更改的字段名称列表。 <{1}}是否存在任何特殊情况会干扰我不知道。
答案 1 :(得分:0)
在js中,