按下保存按钮时没有删除ImageField:Django admin



我有以下文件

型号.py

Class Trip(models.Model)

featured = models.BooleanField(default=False, help_text='Tags this trip as a featured trip.')
top_ten_trip = models.BooleanField(default=False)
header_image = models.ImageField(help_text='This is the image shown when viewing the details for this trip.', upload_to='images/', blank=True, null=True)
map_image = models.ImageField(help_text='The map image for this trip.', upload_to='images/', blank=True, null=True)
.....

等等

管理员.py

类TripAdmin(admin.ModelAdmin):

list_display = ('name', 'code', 'category', 'supplier', 'active', 'featured', 'top_ten_trip',)
search_fields = ('name', 'code',)
list_filter = ('category', 'featured', 'top_ten_trip',)
filter_horizontal = ('countries', 'related_trips',)

该字段在admin中显示为带有浏览按钮,类似于以下内容标题图像:当前:images/coffet_japan.jpg更改:删除此文件

地图图片:当前:images/Map_japan_.jpg更改:删除此文件

现在的问题是:-当我点击删除这个图像文件(images/coffeth_japan.jpg)时,它会被删除,但当我按下管理员上的保存按钮时不会被删除。

我想当我点击删除这个文件并按保存时,它应该会被删除。

我犯了什么错误,还是错过了什么?

提前谢谢。

这是Django 1.3中有意更改的,请参阅Django门票#15224。
您需要自己实现文件删除。查看相关帖子:

  • 当我从数据库/模型中删除对象时,如何让Django Admin删除文件
  • Django管理员:添加一个";删除文件";图像字段或文件字段

大家好,我使用代码段1633 解决了我的问题

http://djangosnippets.org/snippets/1633/。效果很好。

最新更新