I was hunting around the Internet 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 the 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?
Additional information as requested:
Model of blah
:
class Blah(models.Model):
blah_name = models.CharField(max_length=25, unique=True)
slug = models.SlugField()
img01 = models.ImageField(upload_to='scenes/%Y/%m', blank=True)
def __unicode__(self):
return self.blah_name