开发者

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

开发者 https://www.devze.com 2022-12-27 00:05 出处:网络
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 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


Never use super(self.__class__, self)! Try the following example:

class A(object):
    def m(self):
        super(self.__class__, self).m()

class B(A): pass

B().m()

It will fail with the same error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 4, in m
  ... repeated a lot of times ...
RuntimeError: maximum recursion depth exceeded while calling a Python object

Let's see what's going on. You call A.m method for B instance, so self.__class__ is B and super(self.__class__, self).m refers to the same method A.m, so A.m calls itself instead of calling the method of base class. This leads to infinite recursion.


I have tested it on my machine and it works :-) . I used exactly your piece of code. The problem has to be outside of this code.

Please post a snippet how you call/save the form and the declaration of the Model Blah.

Did you overwrite the save method of Model Blah?

0

精彩评论

暂无评论...
验证码 换一张
取 消