If I have a Pizza model and a Topping model, with m2m between them, is there some quick elegant way to ad开发者_JAVA技巧d 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.
You can make your own custom FilterSpec.
Custom Filter in Django Admin on Django 1.3 or below
http://djangosnippets.org/snippets/1963/
http://www.djangosnippets.org/snippets/1051/
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',]
精彩评论