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

后端 未结 7 1276
野性不改
野性不改 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:46

    As I have answered here DjangoFilterBackend with multiple ids, it is now pretty easy to make a filter that accepts list and validates the contents

    For Example:

    
    from django_filters import rest_framework as filters
    
    
    class NumberInFilter(filters.BaseInFilter, filters.NumberFilter):
        pass
    
    class MyFilter(filters.FilterSet):
        id_in = NumberInFilter(field_name='id', lookup_expr='in')
    
        class Meta:
            model = MyModel
            fields = ['id_in', ]
    
    

    This will accept a list of integers from a get parameter. For example /endpoint/?id_in=1,2,3

    0 讨论(0)
提交回复
热议问题