Django 管理员:添加“删除文件”图像字段或文件字段

发布于 2024-08-29 09:13:15 字数 1529 浏览 5 评论 0原文

我在互联网上寻找一种方法,可以轻松地允许用户清空他们在管理中设置的图像字段/文件字段。

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

对我来说真正有趣的是 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

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

当我运行它时,我收到错误

调用 Python 对象时超出最大递归深度

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

当我稍微思考一下时,很明显它只是无限地调用自身导致了错误。我的问题是我不知道我应该这样做的正确方法是什么。 有什么建议吗?

根据要求提供其他信息:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬心 2024-09-05 09:13:15

切勿使用 super(self.__class__, self)!尝试以下示例:

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

class B(A): pass

B().m()

它将失败并出现相同的错误:

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__Bsuper(self. __class__, self).m 引用了同一个方法 Am,因此 Am 调用自身而不是调用基类的方法。这会导致无限递归。

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.

倾城°AllureLove 2024-09-05 09:13:15

我已经在我的机器上测试过它并且它有效:-)。我完全使用了你的代码。
问题必须在这段代码之外。

请发布一段如何调用/保存表单和模型声明的片段Blah

您是否覆盖了 Model Blah 的保存方法?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文