Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django管理员:添加一个;删除文件";用于图像或文件字段的字段_Python_Django_Django Admin_Django Forms_Django Models - Fatal编程技术网

Python Django管理员:添加一个;删除文件";用于图像或文件字段的字段

Python Django管理员:添加一个;删除文件";用于图像或文件字段的字段,python,django,django-admin,django-forms,django-models,Python,Django,Django Admin,Django Forms,Django Models,我在互联网上四处寻找一种方法,让用户能够轻松地清空他们在管理中设置的imagefield/FileField 我找到了这个: 我真正感兴趣的是rfugger在评论中发布的代码: 当我尝试在自己的表单中使用它时,我基本上是将它添加到我的admin.py,它已经有了一个BlahAdmin class BlahModelForm(forms.ModelForm): class Meta: model = Blah remove_img01 = forms.Boolea

我在互联网上四处寻找一种方法,让用户能够轻松地清空他们在管理中设置的imagefield/FileField

我找到了这个:

我真正感兴趣的是rfugger在评论中发布的代码:

当我尝试在自己的表单中使用它时,我基本上是将它添加到我的
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

我已经在我的机器上测试了它,它可以工作:-)。我用的正是你的代码。 问题必须超出此代码的范围

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

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

切勿使用
super(self.\uuu class\uuuuu,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
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“”,第4行,单位为m
... 重复了很多次。。。
RuntimeError:调用Python对象时超出了最大递归深度

让我们看看发生了什么事。您为
B
实例调用
A.m
方法,因此
self.\uuuu class\uuuuuuuu
B
super(self.\uu class\uuuuuuuu,self)。m
引用相同的方法
A.m
,因此
A.m
调用自己而不是调用基类的方法。这会导致无限递归。

调用超级方法看起来不错,您确定没有更多或不同的代码吗?另外,不要使用对象,而要使用“obj”,谢谢您提供的使用obj的提示。我不知道是否应该有更多的代码,这就是所有的链接。链接上的评论者可能会认为,读过这篇文章的人在django/python方面比我熟练得多,而且缺失的部分是显而易见的?嗨,马士基,谢谢你的关注。我同意这听起来像是代码之外的东西。我没有做任何特殊的事情来调用表单,我只是在django admin中使用它。我没有覆盖模型的保存方法。我把模型放在上面。嘿,丹尼斯,谢谢你的回答。昨天我尝试了:obj=super(BlahModelForm,self)。save(*args,**kwargs),结果正如预期的那样。仔细想想,它不应该和使用super(self.\u class\u,self).save(*args,**kwargs)有相同的结果吗?在第一个例子中,我似乎是显式地调用我所在的类的名称,而不是通过self。这里有什么明显的遗漏吗?当你使用self.\u class.\u在BlahModelForm方法中,它不能保证给你BlahModelForm。self可以是另一个类的实例(特别是BlahModelForm的子类/派生类)。这就是丹尼斯的例子所说明的情况。
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