将一些参数添加到FilterSet(django-filter)+一些参数

时间:2017-02-07 21:42:47

标签: django django-forms django-filter django-filters

我有3个型号:

class Category(models.Model):
    name = models.CharField(max_length=200)
    slug = models.SlugField(max_length=70, null=True, blank=True)

class SubCategory(models.Model):
    category= models.ForeignKey(Category, on_delete=models.CASCADE)
    name = models.CharField(max_length=200, )

class Products(models.Model):
    user= models.ForeignKey(User, on_delete=models.CASCADE)
    category= models.ForeignKey(Category, on_delete=models.CASCADE)
    subcategory = models.CharField(max_length=200, null=True, blank=True)

我有一个接收requestcategory.slug

的视图
def category_list(request, slug):
    category = Category.objects.get(slug=slug)
    products = ProductFilter(request.GET, queryset=Products.objects.filter(category=category)

    return render(request, 'products/category_list.html', {"products":products, 'category': category})

渲染时,我收到QuerySet过滤到Category

我想将category.id发送给ProductsFilter,并从数据库中获取动态选择

class ProductsFilter(django_filters.FilterSet):
    subcategory= django_filters.ChoiceFilter(lookup_expr='iexact', choices=TEST, required=False)       

    class Meta:
        model = Products
        fields = {
            "subcategory",
        }

想要将choices=TEST更改为choices=list(SubCategory.objects.filter(category_id=category.id)

这可能吗?

2 个答案:

答案 0 :(得分:0)

您可以使用Function(obj.script)(); 方法处理此问题。像下面这样的东西(注意我还没有测试过,可能需要一些小问题):

FilterSet.__init__

答案 1 :(得分:0)

@Sherpa的回答只有两个小问题。首先,应将fields替换为filters。其次,您不能使用+=运算符,而必须直接分配给过滤器的extra。 这是我两种不同方式的工作代码

class LayoutFilterView(filters.FilterSet):
    supplier = filters.ChoiceFilter(
        label=_('Supplier'), empty_label=_("All Suppliers"),)

    def __init__(self, *args, **kwargs):
        super(LayoutFilterView, self).__init__(*args, **kwargs)

        # First Method
        self.filters['supplier'].extra['choices'] = [
            (supplier.id, supplier.name) for supplier in ourSuppliers(request=self.request)
        ]

        # Second Method
        self.filters['supplier'].extra.update({
            'choices': [(supplier.id, supplier.name) for supplier in ourSuppliers(request=self.request)]
        })

最初发布的here