Trying to solve an interesting problem right now.
I have a Django model with an image field that is not required, but is set to the default when instantiating a new model.
class Product(models.Model): image = models.ImageField(upload_to='/image/directory/', default='/default/image/path/', blank=True)
I also have a ModelForm based on this model that includes an image field and has some custom validation.
class ProductForm(forms.ModelForm): class Meta: model = Product fields = ('image',) def clean_image(self): image = self.cleaned_data.get('image', False) if image:
The problem is that in the documents , calling is_valid() in the validation of the ModelForm trigger model in addition to validating the form, so when the user submits the model form without an image, my form validation code tries to validate the model image by default, and not just do nothing as intended.
How can I make it do nothing if the form itself does not matter for the image field?
source share