Django admin:添加“删除文件” Image或FileField的字段 [英] Django admin: Add a "remove file" field for Image- or FileFields

查看:817
本文介绍了Django admin:添加“删除文件” Image或FileField的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在互联网上狩猎,以方便用户清理他们在管理员中设置的图像场/文件夹。

I was hunting around the Internet for a way to easily allow users to blank out imagefield/filefields they have set in the admin.

我发现这个: http://www.djangosnippets.org/snippets/894/

这里真正有趣的是在 rfugger 的评论中发布的代码:

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

当我尝试以我自己的形式使用它时,我基本上将其添加到我的 admin.py 已经有一个 BlahAdmin

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


在此行调用Python对象时,最大递归深度超出

maximum recursion depth exceeded while calling a Python object

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?

请求的其他信息:

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


推荐答案

不要使用 super(self .__ class__,self)!尝试以下示例:

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

我们来看看发生了什么。您为 B 实例调用 Am 方法,所以 self .__ class __ B super(self .__ class__,self).m 指的是相同的方法 Am ,所以 Am 调用自己而不是调用基类的方法。这导致无限递归。

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.

这篇关于Django admin:添加“删除文件” Image或FileField的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆