Jon*_*Cox 5 django django-models django-admin
在模型管理对象上,我有一个可调用函数,它返回 True 或 False。我希望能够使用这个可调用对象来过滤列表中显示的内容(即 list_filter)。但是,下面的代码不起作用,因为您只能list_filter在字段上使用:
...
class FooAdmin(admin.ModelAdmin):
...
list_filter['bar']
def bar(self, obj):
x = ... #something boolean
return x
...
Run Code Online (Sandbox Code Playgroud)
有什么方法可以使用 True/False callable 来过滤 admin 中的列表?或者如果你想要这个功能,你是否必须对你的数据进行非规范化?
我注意到在开发文档中,这现在是可能的:https : //docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.list_filter
但是在 1.3 文档(我使用的 Django 版本)中它没有提到这一点:https : //docs.djangoproject.com/en/1.3/ref/contrib/admin/#django.contrib.admin.ModelAdmin。 list_filter所以我假设我不能在我的项目中使用新功能:-(
如果您可以以某种方式bar用 ORM 双下划线查找路径来表达函数的操作,那么您也许可以在 Django 1.3 中创建 FilterSpec
看django.contrib.admin.filterspecs
这些类负责生成过滤器选择列表并为 url 准备查询字符串值等。据我所知,它们通过提供一个field_path属性来工作,管理代码的其他部分使用该属性来过滤更改列表查询集。
有关自定义 FilterSpec 的示例,请参阅:
http://djangosnippets.org/snippets/2644/