Opened 11 years ago

Closed 8 years ago

#7968 closed (duplicate)

ImageField/FileField behaviour on ModelForms

Reported by: sime Owned by: Gabriel Hurley
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: no UI/UX: no


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 11 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 11 years ago by sime

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

comment:1 Changed 11 years ago by sime

Triage Stage: UnreviewedDesign 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 10 years ago by Michael Stevens <mstevens@…>

This would have really helped me recently.

comment:3 Changed 8 years ago by Gabriel Hurley

Owner: changed from nobody to Gabriel Hurley
Patch needs improvement: set
Status: newassigned
Triage Stage: Design decision neededAccepted

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 8 years ago by Gabriel Hurley

Resolution: duplicate
Status: assignedclosed

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