Opened 7 years ago

Closed 5 years ago

#7968 closed (duplicate)

ImageField/FileField behaviour on ModelForms

Reported by: sime Owned by: gabrielhurley
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: UI/UX:


When an ImageField is left blank on a ModelForm, and the underlying instance already has an image, the image remains. It isn't overwritten with None (removed).

This behaviour is good design :-) But it isn't documented, it isn't consistent with the behaviour of the other fields, and it doesn't provide a way of removing an image (I recall this was a problem with admin, not sure if it still is).

I suggest we add a note to the ModelForms doc to let people know that blank ImageFields won't remove existing images. Also, we might want to consider automatically adding an extra non-required BooleanField to the ModelForm, maybe remove_FIELDNAME.

Attachments (1)

modelforms-doc-imagefields.diff (1.2 KB) - added by sime 7 years ago.
Brief note about existing behaviour, and a suggestion on how to handle removing existing images/files

Download all attachments as: .zip

Change History (5)

Changed 7 years ago by sime

Brief note about existing behaviour, and a suggestion on how to handle removing existing images/files

comment:1 Changed 7 years ago by sime

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

Here is how I'm adding an extra 'remove image' tick --

class MyModel(models.Model):
    name = models.CharField(max_length=100)
    image = models.ImageField()

class MyForm(forms.ModelForm):
    class Meta:
        model = MyModel
    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        if self.instance.image:
            self.fields['remove_image'] = forms.BooleanField(required=False)
    def save(self):
        d = self.cleaned_data
        if self.instance and d.get('remove_image'):
            self.instance.image = ''
        super(MyForm, self).save()

It would be great if ModelForm would give me the remove_image field automatically.

comment:2 Changed 6 years ago by Michael Stevens <mstevens@…>

This would have really helped me recently.

comment:3 Changed 5 years ago by gabrielhurley

  • Owner changed from nobody to gabrielhurley
  • Patch needs improvement set
  • Status changed from new to assigned
  • Triage Stage changed from Design decision needed to Accepted

Adding a note about this in the documentation is fine. Adding a remove_image field to ModelForm is a completely different issue and should be its own ticket, so I'm disregarding that comment.

The patch language needs a little work, but I'll have to think on the right direction for improvement. I'll add this ticket to my list to finish off.

comment:4 Changed 5 years ago by gabrielhurley

  • Resolution set to duplicate
  • Status changed from assigned to closed

Since #7048 was fixed in [13968] (added a ClearableFileInput widget), this ticket is now a duplicate. Thanks to jezdez, jarrow and Carl Meyer for making my job easier ;-)

Note: See TracTickets for help on using tickets.
Back to Top