我在使用Django 11.1时遇到问题。我为文件上传添加了ManyToMany关系,但是在Admin中没有发送文件的选项。
在此系统中,我具有课程的注册信息,并且需要发送文件进行下载。每个课程可能有几个文件,所以有一个ManyToMany接口。
下面是管理员如何显示字段的图像。
models.py
class PalestraFile(models.Model):
file = models.FileField(
upload_to=path_and_rename('uploaded_files/palestra/'),
blank=True,
verbose_name="Arquivo da programação",
help_text="(.pdf, .doc, .txt, .png, .jpg)")
class Palestra(Programacao):
palestrante = models.ForeignKey(Palestrante,
null=True,
blank=True,
limit_choices_to={'ativo': True})
palestrantes = models.ManyToManyField(
Palestrante,
related_name='palestra_palestrantes',
blank=True,
limit_choices_to={'ativo': True}
)
file = models.ManyToManyField(
PalestraFile,
related_name='palestra_palestrafiles',
blank=True,
verbose_name="Arquivos da palestra")
class Meta:
verbose_name = 'Palestra'
verbose_name_plural = 'Palestras'
def __str__(self):
return '%s | %s ' % (date(self.dia, "d/m"), self.tema)
admin.py
from django.contrib import admin
from .models import Palestrante, Palestra, PalestraFile
from .forms import PalestranteFormAdmin
@admin.register(Palestrante)
class PalestranteAdmin(admin.ModelAdmin):
form = PalestranteFormAdmin
list_display = ['nome', 'email', 'slug', 'ativo']
prepopulated_fields = {"slug": ("nome",)}
# actions = ['compress_uploads']
def compress_uploads(self, request, queryset):
for obj in queryset:
obj.save()
compress_uploads.short_description = "Comprimir Imagens de Uploads"
@admin.register(Palestra)
class PalestraAdmin(admin.ModelAdmin):
search_fields = (
'tema',
'palestrante__nome',
'dia',
'evento__nome',
'evento__local'
)
list_display = [
'evento',
'palestrante',
'dia',
'tema',
'sala',
'horario_inicio',
'horario_termino',
'status'
]
filter_horizontal = ['palestrantes']
save_as = True
forms.py
from django import forms
from redactor.widgets import RedactorEditor
from .models import Palestrante
class PalestranteFormAdmin(forms.ModelForm):
class Meta:
model = Palestrante
fields = ['nome', 'slug', 'email', 'foto', 'cargo', 'curriculo', 'linked_in', 'ativo',]
widgets = {
'curriculo': RedactorEditor(),
}