Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django admin - use filter horizontal on a many to many field with intermediate table

Is it possible to use filter_horizontal for a field which is a ManyToManyField with an intermediate table such as with those without intermediate tables?

e.g:

class A(models.Model):
    f1 = models.ManyToManyField(B)
    f2 = models.ManyToManyField(C, through='T')

class B(models.Model):
    pass

class C(models.Model):
    pass

class T(models.Model):
    a = models.ForeignKey(A)
    c = models.ForeignKey(C)

class AAdmin(admin.ModelAdmin):
    filter_horizontal = ('f1', 'f2', )
like image 815
se7entyse7en Avatar asked Oct 18 '25 23:10

se7entyse7en


1 Answers

When you have a many to many field with an intermediate table, it's not possible to display the regular, filter horizontal, or filter vertical widget. The reason for this is that the intermediate table may have extra fields that can not be displayed in those widgets.

It is possible to display the related model as an inline. See the docs on working with many-to-many intermediary models for more info.

like image 158
Alasdair Avatar answered Oct 21 '25 05:10

Alasdair