5.10.drf的过滤

通过属性过滤

goods/views.py

class GoodsCategoryViewSet(ListModelMixin, GenericViewSet):
    """
    获取商品类型列表
    """
    queryset = GoodsCategory.objects.all()
    ...

通过函数过滤

goods/views.py

class GoodsCategoryViewSet(ListModelMixin, GenericViewSet):
    """
    获取商品类型列表
    """
    queryset = GoodsCategory.objects.all()
    ...
    def get_queryset(self):
        return GoodsCategory.objects.filter(category_type__gt=1)

更复杂的函数过滤

goods/views.py

django-filter

简化传统filter

goods/views.py

存在问题:无法模糊匹配

自定义django-filter

goods/filters.py

goods/views.py

Last updated

Was this helpful?