我在網上搜索的方式來輕鬆讓用戶消除他們已設置在管理員的imagefield/filefields。Django管理員:添加一個「刪除文件」字段的圖像或FileField
我發現這個: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
當我運行它,我得到的錯誤
maximum recursion depth exceeded while calling a Python object
在這一行:
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
調用超級方法看起來不錯,你確定沒有更多或不同的代碼?也代替'object'使用'obj' – 2010-04-17 05:05:23
感謝提示使用obj。 我不知道是否應該有更多的代碼,這就是所有鏈接。鏈接上的評論者可能會認爲有人閱讀過django/python比我更精通,缺少的位是顯而易見的? – 2010-04-17 17:44:51