Opened 16 years ago

Closed 14 years ago

#7968 closed (duplicate)

ImageField/FileField behaviour on ModelForms

Reported by: sime Owned by: Gabriel Hurley
Component: Documentation Version: dev
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

Description

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 16 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)

by sime, 16 years ago

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

comment:1 by sime, 16 years ago

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 by Michael Stevens <mstevens@…>, 15 years ago

This would have really helped me recently.

comment:3 by Gabriel Hurley, 14 years ago

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

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