# coding=utf-8
import django_filters
from .models import Commodity
from django.db.models import Q

class CommodityFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(field_name='name', lookup_expr='icontains')
    category = django_filters.CharFilter(method='find_category')

    def find_category(self,queryset, *args):
        queryset = queryset.filter(Q(category_id=args[1]) | Q(category__category_id=args[1]) | Q(category__category__category_id=args[1]))
        return queryset

    class Meta:
        model = Commodity
        fields = '__all__'
