开发者

Is there an elegant way to have a list_filter for a M2M field in the Django admin?

开发者 https://www.devze.com 2022-12-31 13:34 出处:网络
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 c

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',]
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号