Opened 17 years ago
Closed 15 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)
Change History (5)
by , 17 years ago
| Attachment: | modelforms-doc-imagefields.diff added |
|---|
comment:1 by , 17 years ago
| Triage Stage: | Unreviewed → 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:3 by , 15 years ago
| Owner: | changed from to |
|---|---|
| Patch needs improvement: | set |
| Status: | new → assigned |
| Triage Stage: | Design decision needed → 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 by , 15 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | assigned → closed |
Brief note about existing behaviour, and a suggestion on how to handle removing existing images/files