[Answered ]-Filtering objects through a range of object attribute in Django-rest-framework

0👍

Try this in CommodityFilter class:

class CommodityFilter(filters.FilterSet):
    start_date = django_filters.NumberFilter(
        name='commodity_year', lookup_expr='gte')
    end_date = django_filters.NumberFilter(
        name='commodity_year', lookup_expr='lte')

    class Meta:
        model = Commodity
        fields =  [ 'country','commodity_name', 'commodity_type','start_date','end_date']

2👍

You can filter with a range using less than equal to and greater than equal to.

queryset = Commodity.objects.filter(commodity_year__gte=1999,
                                    commodity_year__lte=2014)

The above will give you a queryset where commodity_year is less than 2014 and greater than 1999. Read more about gt at django docs

Note: I don’t think filter class is needed as it is a very simple query

Leave a comment