简体   繁体   English

django管理页面中用于外键字段的list_filter

[英]list_filter in django admin page for foreign key field

use foreign fields in list_filter of django admin page 在Django管理页面的list_filter中使用外部字段

Suppose i have models 假设我有模型

   class Company():
         name varchar(50)
         field1 varchar(50)

   class Client()
          name varchar(50)
          company ForeignKey(Company)

Now how can i implement filter on Client model for field1 in django admin page 现在如何在Django管理页面中为field1的客户端模型实施过滤器

I tried list_filter = ['company_ field1', 'company _name'] but didn't work. 我尝试了list_filter = ['company_ field1','company _name'],但是没有用。 Any solutions? 有什么办法吗?

I'm using django 1.2 version. 我正在使用django 1.2版本。

Thanks in advance 提前致谢

This can't be done in Django 1.2. 这在Django 1.2中无法完成。

Starting with 1.3 you can use that standard `relatedModel__field' syntax (see the docs ). 从1.3开始,您可以使用标准的“ relatedModel__field”语法(请参阅docs )。 Note that you have to use a double underscore for this to work. 请注意,您必须使用双下划线才能起作用。

In the current dev version you could to even more complex things be using a subclass of SimpleListFilter which pratically allows you to do anything you like ;) 在当前的开发版本中,您甚至可以使用SimpleListFilter的子类来做更复杂的事情,该子类通常允许您执行任何您喜欢的事情;)

With Django 1.2, there is a hacky work around I've used for this sort of thing. 在Django 1.2中,我已经使用了一些棘手的方法来解决这类问题。

In your database, define a view on a join of the two tables with the foreign key relationship. 在数据库中,使用外键关系在两个表的联接上定义一个视图。 Then make Django use the view instead of the real table. 然后让Django使用视图而不是实际表。 Put triggers on the view to redirect database writes to the correct table. 在视图上放置触发器,以将数据库写入重定向到正确的表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM