[英]Rename Filter Label Model Admin Django
Is it possible to change this label in the list filter from model admin without creating a custom filter?是否可以在不创建自定义过滤器的情况下从模型管理员更改列表过滤器中的此标签?
I'm using the same external model for 2 different fields and user needs to filter the table using both fields (separately or combining them).我对 2 个不同的字段使用相同的外部模型,用户需要使用这两个字段(单独或组合)过滤表。
Without the rename, the Admin view will have 2 filters with same name applying to different fields.(see picture) Not the best ux.如果没有重命名,管理视图将有 2 个具有相同名称的过滤器应用于不同的字段。(见图)不是最好的 ux。
The first column is a field not in this model it is calling related model field as a list_display and listFilter:第一列是不在此模型中的字段,它将相关模型字段称为 list_display 和 listFilter:
list_display = ([...] 'contract_gjl_entity', 'gjl_paying_entity', [...])
list_filter = (
[...]
('gjl_paying_entity__name', DropdownFilter),
('contract__gjl_entity__name', DropdownFilter),
[...]
# Function that get the entities using contract id field and join them in a csv text to show in grid
def contract_gjl_entity(obj):
contract_id = obj.contract_number
contract = Contract.objects.get(id=contract_id)
return ", ".join(str(seg) for seg in contract.gjl_entity.all())
What I need is to rename the 'By GJL Entity' --> 'By Contract Entity' and the other 'By GJL Entity' --> 'By Paying Entity'我需要重命名“按 GJL 实体”->“按合同实体”和另一个“按 GJL 实体”->“按付款实体”
from django.contrib import admin
def custom_titled_filter(title):
class Wrapper(admin.FieldListFilter):
def __new__(cls, *args, **kwargs):
instance = admin.FieldListFilter.create(*args, **kwargs)
instance.title = title
return instance
return Wrapper
After that in your ModelAdmin class:
list_filter = (
('fieldname', custom_titled_filter('My Custom Title')),
'plain_field',
...
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.