Django 管理员:添加“删除文件”图像字段或文件字段
我在互联网上寻找一种方法,可以轻松地允许用户清空他们在管理中设置的图像字段/文件字段。
我发现了这个: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
切勿使用 super(self.__class__, self)!尝试以下示例:
它将失败并出现相同的错误:
让我们看看发生了什么。您为
B
实例调用Am
方法,因此self.__class__
是B
和super(self. __class__, self).m
引用了同一个方法Am
,因此Am
调用自身而不是调用基类的方法。这会导致无限递归。Never use
super(self.__class__, self)
! Try the following example:It will fail with the same error:
Let's see what's going on. You call
A.m
method forB
instance, soself.__class__
isB
andsuper(self.__class__, self).m
refers to the same methodA.m
, soA.m
calls itself instead of calling the method of base class. This leads to infinite recursion.我已经在我的机器上测试过它并且它有效:-)。我完全使用了你的代码。
问题必须在这段代码之外。
请发布一段如何调用/保存表单和模型声明的片段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?