I try to create a link to download a file in the admin but it does not work
my model
class Event(models.Model):
......
file = models.FileField(_('fichier'), upload_to='medias', null=True, blank=True)
def file_(self):
if self.file:
return "<a href='%s'>download</a>" % (self.file.url,)
else:
return "No attachment"
my admin :
class EventAdmin(admin.ModelAdmin):
list_display = ('title', 'start', 'end', 'user', 'fin', 'frequency', 'file_',)
fieldsets = (
(None, {
'fields': ('title','start', 'end', 'is_cancelled', 'calendar', 'user', 'description', ('frequency', 'fin' ), 'activated', 'file_',)
}),
I get the error :
Exception Value:
'EventAdmin.fieldsets[0][1]['fields']' refers to field 'file_' that is missing from the form.
what is this problem ?
You should define your admin like this:
class EventAdmin(admin.ModelAdmin):
list_display = ('title', 'start', 'end', 'user', 'fin', 'frequency', 'file_link',)
fieldsets = (
(None, {
'fields': ('title','start', 'end', 'is_cancelled', 'calendar', 'user', 'description', ('frequency', 'fin' ), 'activated', 'file',)
}),
def file_link(self, obj):
if obj.file:
return "<a href='%s'>download</a>" % (obj.file.url,)
else:
return "No attachment"