django admin: Add a "remove file" field for Image- or FileFields

Posted by w- on Stack Overflow See other posts from Stack Overflow or by w-
Published on 2010-04-17T03:49:25Z Indexed on 2010/04/17 3:53 UTC
Read the original article Hit count: 708

I was hunting around the net for a way to easily allow users to blank out imagefield/filefields they have set in the admin.
I found this
http://www.djangosnippets.org/snippets/894/
What was really interesting to me here was the code posted in the comment by rfugger

remove_the_file = forms.BooleanField(required=False)

def save(self, *args, **kwargs):
    object = super(self.__class__, self).save(*args, **kwargs)
    if self.cleaned_data.get('remove_the_file'):
        object.the_file = ''
    return object

When i try to use this in my own form I basically added this to my admin.py which already had a BlahAdmin

class BlahModelForm(forms.ModelForm):
    class Meta:
        model = Blah

    remove_img01 = forms.BooleanField(required=False)

    def save(self, *args, **kwargs):
        object = super(self.__class__, self).save(*args, **kwargs)
        if self.cleaned_data.get('remove_img01'):
            object.img01 = ''
        return object

when i run it I get this error

maximum recursion depth exceeded while calling a Python object

at this line

object = super(self.__class__, self).save(*args, **kwargs)

When i think about it for a bit, it seems obvious that it is just infinitely calling itself causing the error. My problem is i can't figure out what is the correct way i should be doing this. Any suggestions?

thanks

© Stack Overflow or respective owner

Related posts about django-admin

Related posts about django-imagefield