如何在admin list_filter中引用相关模型的字段? [英] How do you reference related models' fields in the admin list_filter?

查看:176
本文介绍了如何在admin list_filter中引用相关模型的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型:

OrderInfo与打印是一对一的,它是一个多对一的绘画,它本身是一个多对一的,一个有俱乐部。

OrderInfo is one-to-one with Print, which is a Many-to-One with Painting, which itself is a Many-to-One with Club.

class OrderInfo(models.Model):
    print_ordered = models.OneToOneField(Print, blank=True)

class Print(models.Model):
    painting = models.ForeignKey(Painting)

class Painting(models.Model):
    club = models.ForeignKey(Club)

在我的OrderInfoAdmin中,我想要排序由相关的俱乐部,但我无法弄清楚这样做的语法。

In my OrderInfoAdmin, I'd like to be able to sort by the related Club, but I can't figure out the syntax to do this.

我尝试过这个,但不行:

I tried this, but it does not work:

class OrderInfoAdmin(admin.ModelAdmin):
    list_filter = ['print_ordered__painting__club']

,谢谢提前!

推荐答案

list_filter 命令用于过滤排序。你想要这里记载的订单命令: http://docs.djangoproject.com/en/dev/ref/models/options/#ordering 。如果建模 Meta命令允许通过外键排序,我从文档中不清楚。 ( filter() queryset函数绝对可以: http://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by-fields

The list_filter command is for filtering not ordering. You want the ordering command which is documented here: http://docs.djangoproject.com/en/dev/ref/models/options/#ordering. It's not clear to me though from the documentation if the modeling Meta command allows ordering through foreign keys. (The filter() queryset function definitely does: http://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by-fields)

我发现管理员使用深层次模型有一些限制。例如,您可以在其父页面上内联子模型,但不能内联孙子。另外,默认情况下, list_filter 命令仅适用于建模表中的字段。在您的示例中,您无法过滤 OrderInfo admin中的打印的字段。

I've found the admin has some limitations working with deeply hierarchical models. For example, you can inline a child model on its parent's page, but you can't inline an grandchild. Also, by default the list_filter command only works on fields in the modeled table. In your example, you couldn't filter on the fields of Print in the OrderInfo admin for example.

这篇关于如何在admin list_filter中引用相关模型的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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