19👍
✅
Just solved it in pretty simple way. Adding the answer here in case it’s helpful to anyone else.
…a model form instance bound to a model object will contain a
self.instance
attribute that gives model form methods access to that specific model instance.
So rather than check if the ModelForm
has an image value, I just check whether the image value has changed from the saved instance. The form validation now looks like this:
class ProductForm(forms.ModelForm):
class Meta:
model = Product
fields = ('image',)
def clean_image(self):
image = self.cleaned_data.get('image', False)
if not self.instance.image == image:
# validate image
return None
Problem solved!
Source:stackexchange.com