Django restful framework의 Filter 사용자 정의 필터

1184 단어
class AlertFilter(django_filters.rest_framework.FilterSet):
    
     .......
    to_bearer_service = django_filters.rest_framework.CharFilter(method='to_bearer_service_filter', label='bearer_service')

    class Meta:
        model = Alert
        fields = [......, 'to_bearer_service']

    @staticmethod    #                        ,                 
    def to_bearer_service_filter(queryset, name, value):
        Q_id = Q()
        for i in value.split(","):
            if i == "0":
                Q_id.add(Q(**{'to_bearer_service':None}), Q.OR)
            else:
                Q_id.add(Q(**{'to_bearer_service_id': int(i)}), Q.OR)
        queryset = queryset.filter(Q_id)
        return queryset
  
  
    def to_bearer_service_filter(self, queryset, name, value):
        Q_id = Q()
        for i in value.split(","):
            if i == "0":
                Q_id.add(Q(**{'to_bearer_service':None}), Q.OR)
            else:
                Q_id.add(Q(**{'to_bearer_service_id': int(i)}), Q.OR)
        queryset = queryset.filter(Q_id)
        return queryset

좋은 웹페이지 즐겨찾기