2
votes

If I have a Pizza model and a Topping model, with m2m between them, is there some quick elegant way to add to the admin list page for either of them a list filter for all pizzas which contain a certain topping / all toppings that are contained in a certain pizza?

The built-in list_filter doesn't support m2m fields so I'm looking for some workaround to allow this sort of filtering.

2

2 Answers

0
votes

Django 1.5+ supports m2m fields:

class Post(models.Model):
    categories = models.ManyToManyField(Category,
                                        verbose_name=_("Categories"),
                                        blank=True,
                                        related_name="posts")

class PostAdmin(admin.ModelAdmin):
    list_filter = ['categories__title',]