将当前过滤器选择输入到Djang中的另一个自定义SimpleListFilter

2024-10-05 14:22:25 发布

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

我正在尝试更改一个筛选器的提示,以响应另一个筛选器中所做的当前选择。对于如何将AttributeCategoryFilter的当前选定值传递到AttributeFilter中,我非常困惑。我使用的是django1.4-dev,试图找出是否应该为此使用RelatedFieldListFilter。看起来这些特征还很年轻,还没有在野外流传的例子。在

    class AttributeCategoryFilter(SimpleListFilter):
        title = _('Attribute Category')
        parameter_name = 'attribute_category'
        def lookups(self, request, model_admin):
            attributes = Attribute.objects.filter(parent_attribute=None)
            prompts = []
            for attribute in attributes:
                prompts.append((attribute.title, _(str(attribute.title))))
            return prompts
        def queryset(self, request, queryset):
            if self.value():
                return queryset.filter(attribute__category=self.value())
            else:
                return queryset


    class AttributeFilter(SimpleListFilter):
        title = _('Attribute Title')
        parameter_name = 'attribute_title'
        def lookups(self, request, model_admin):
            desired_category =  # Needs to be a reference to the selected value in the AttributeCategoryFilter above
            attributes = Attribute.objects.filter(category=desired_category).exclude(parent_attribute=None)
            prompts = []
            for attribute in attributes:
                prompts.append((attribute.title, _(str(attribute.title))))
            return prompts
        def queryset(self, request, queryset):
            if self.value():
                return queryset.filter(attribute__title=self.value())
            else:
                return queryset


    class ValueAdmin(admin.ModelAdmin):
        list_display = ('package', 'attribute', 'presence', 'text', 'modified', 'created')
        list_filter = ('package', AttributeCategoryFilter, AttributeFilter, 'presence', 
            'attribute__admin_approved', 'attribute__dtype', 'modified')
        search_fields = ('package', 'attribute', 'text')
        list_display_links = ('package', )
        list_editable = ('presence', 'text')
        list_per_page = 20000
    admin.site.register(Value, ValueAdmin)   

Tags: selfreturnadmintitlevaluerequestdefattribute
2条回答

我在找别的东西的时候发现了你的问题。在

以下是我所做的,只显示在选定游戏中有角色的玩家:

^{1}$

看来这也是丹·克拉松的建议。在

提示:出于安全原因,将id放入查询参数通常被认为是不可能的。在

这是对我有效的方法。。。“TypeListFilter”只有在使用“Category”筛选器后才可见,然后显示作为所选类别“subtype”的所有条目。“特殊情况”黑客进一步深入,确保过滤器消失时,用户选择另一个类别。 “\u class”参数增加了一些额外的灵活性。我使用同一个过滤器与不同但相关的类型类,只需覆盖这一个参数。把它换成管理模型要筛选的类。在

^{1}$

相关问题 更多 >