Django管理可以利用rest框架过滤器吗?

2024-06-02 09:01:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经设置了要使用的模型:

import rest_framework_filters as filters

class FoundationIPFilter(filters.FilterSet):
  foundation_ip_team_group = 
  filters.RelatedFilter(FoundationIPTeamGroupFilter, name='foundation_ip_team_group', queryset=FoundationIPTeamGroup.objects.all(), lookups='__all__')
technology = filters.RelatedFilter(TechnologyFilter, name='technology', queryset=Technology.objects.all(), lookups='__all__')

class Meta:
    model = FoundationIP
    fields = '__all__'

当我使用restapi在FoundationIP上进行复杂的外键过滤时,一切正常。也就是说,这是有效的:

<myurl>/tracker/foundationip/?technology__name=sec11lpp

当我在管理界面中使用相同的筛选时,会返回所有结果,或者出现错误“filtering by not allowed”

<myurl>/admin/tracker/foundationip/?technology__name=sec11lpp

我认为这是因为管理员不知何故没有使用与rest\u框架相同的过滤器,即没有加载我在rest\u框架中的过滤器过滤器.py. 我怎样才能让管理员做到这一点(使用相同的过滤器)?你知道吗

我的管理模型看起来像:

class FoundationIPAdmin(admin.ModelAdmin):
 list_display = ["id", "name", "technology", "foundation_ip_team_group", "design_type"]
 list_display_links = ["id"]
 list_filter = ["name", "technology", "foundation_ip_team_group"]
 list_editable = ["name", "design_type", "foundation_ip_team_group"]
 list_max_show_all = 200
 class Meta:
    model = FoundationIP

Tags: name模型iprest过滤器groupallfilters