Possible to do an `in` `lookup_type` through the django-filter URL parser?

后端 未结 7 1305
野性不改
野性不改 2020-12-09 17:52

I\'m using django-filter with django-rest-framework and I\'m trying to instantiate a filter that accepts lists of numbers for filtering the query set down

c         


        
7条回答
  •  被撕碎了的回忆
    2020-12-09 18:33

    I know this is an old post, but there is now a better solution. The change that makes it correct is posted here.

    They added a BaseInFilter and a BaseRangeFilter. The documentation is here.

    Big picture, BaseFilter checks for CSV, and then when mixed with another filter it does what you are asking. Your code can now be written like:

    class NumberInFilter(filters.BaseInFilter, filters.NumberFilter):
        pass
    
    class MyModelViewSet(viewsets.ModelViewSet):
        ids = NumberInFilter(name='id', lookup_expr='in')
    
        class Meta:
            model = MyModel
            fields = ['ids']
    

提交回复
热议问题