Custom inline list_filter in django admin
I was wondering if it's possible to have a filter over inline fields in the change_list template.
I have this models:
class VisitaVirtual(models.Model):
poi = models.ForeignKey(Poi)
cliente = models.ForeignKey(Cliente, verbose_name=_(u'Cliente'))
titulo = models.CharField(_(u'Título'), max_length=200)
observacoes = models.TextField(_(u'Observações'), null=True, blank=True)
exclusivo = models.BooleanField(_(u'Exclusivo'), default=False)
remover = models.BooleanField(_(u'Remover'), default=False, null=True, blank=True)
created = models.DateTimeField(_(u'Criado em'), auto_now_add=True, editable=False)
updated = models.DateTimeField(_(u'Modificado em'), auto_now=True, editable=False)
class FicheiroVisitaVirtual(models.Model):
visita_virtual = models.ForeignKey(VisitaVirtual, verbose_name=_(u'Visita virtual'))
ficheiro = models.FileField(_(u'Ficheiro'), upload_to='multimedia/visitas_virtuais/ficheiros/')
tipo = models.CharField(_(u'Tipo'), max_length=10, null=True, blank=True, choices=开发者_如何学运维TIPO_VV)
linguagem = models.CharField(_(u'Linguagem'), choices=LANGUAGES, max_length=2)
created = models.DateTimeField(_(u'Criado em'), auto_now_add=True, editable=False)
updated = models.DateTimeField(_(u'Modificado em'), auto_now=True, editable=False)
and my admin.py looks like this:
class FicheiroVisitaVirtualModelInlines(admin.TabularInline):
model = FicheiroVisitaVirtual
extra = 3
list_filter = ['tipo']
class VisitaVirtualAdmin(admin.ModelAdmin):
list_display = ['titulo', 'cliente', 'remover', 'exclusivo', 'created', 'updated']
list_filter = ['created']
search_fields = ['titulo']
ordering = ['titulo']
raw_id_fields = ['poi', 'cliente']
inlines = [FicheiroVisitaVirtualModelInlines]
I need to have a filter in tipo field which so I can filter VisitaVirtual which have FicheiroVisitaVirtual of some time.
Is it possible?
You can use fields of related models in list_filter
using the <related_manager>__<related field>
syntax:
class VisitaVirtualAdmin(admin.ModelAdmin):
list_display = ['ficheirovisitavirtual_set__tipo', ]
(...)
精彩评论