我有一张models.py
,看起来像这样
class Product(models.Model):
name = models.CharField(max_length = 255, unique = True, null = False, blank = False)
brand = models.ForeignKey(Brand, on_delete = models.SET_NULL, null = True, blank = True, related_name = 'products')
class Brand(models.Model):
name = models.CharField(max_length = 255, unique = True, null = False, blank = False)
我想过滤基于Brand
型号的name
字段的产品数据.我的views.py
号是这样的
class ProductAPIView(generics.GenericAPIView):
serializer_class = ProductSerializer
queryset = Product.objects.all()
filter_backends = (DjangoFilterBackend,)
filterset_class = ProductFilter
def get(self, request):
products = self.filter_queryset(self.get_queryset())
serializer = self.serializer_class(products, many = True)
return Response(serializer.data)
我的filters.py
是这样的
class ProductFilter(FilterSet):
class Meta:
model = Product
fields = {
'brand__name' : ['iexact', ]
}
无论我为brand
发送什么查询参数,结果都是整个列表,而不是过滤所需的列表.
我是不是漏掉了什么?提前谢谢你