Django:list_filter 和外键字段 [英] Django: list_filter and foreign key fields

查看:25
本文介绍了Django:list_filter 和外键字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Django 不支持从 list_display 或 list_filter(例如 foo__bar)获取外键值.我知道您可以创建一个模块方法作为 list_display 的解决方法,但是我将如何为 list_filter 做同样的事情?谢谢.

Django doesn't support getting foreign key values from list_display or list_filter (e.g foo__bar). I know you can create a module method as a workaround for list_display, but how would I go about to do the same for list_filter? Thanks.

推荐答案

Django 支持带有外键字段的 list_filter

Django supports list_filter with foreign key fields

# models.py:
class Foo(models.Model):
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name

class Bar(models.Model):
    name = models.CharField(max_length=255)
    foo = models.ForeignKey(Foo)

# admin.py:
class BarAdmin(admin.ModelAdmin):
    list_filter = ('foo__name')

来自文档:list_filter 中的字段名称也可以使用 __ 查找跨越关系

From documentation: Field names in list_filter can also span relations using the __ lookup

这篇关于Django:list_filter 和外键字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆