2017-04-06 41 views
1

我有一個自定義Django過濾器,我想獲得filter_location函數內的「距離」值,任何想法如何實現?我如何從Django過濾器集合中的另一個過濾器獲取值

想要這樣做的原因,我正在做一個外部的API調用來獲取數據,同時需要這兩個值。

class ListingFilter(django_filters.FilterSet): 

    def filter_location(self, queryset, value): 
     # Want to get the distance filter value here 
     if value: 
      #Location value 


    def filter_distance(self, queryset, value): 
     pass 

    distance = django_filters.CharFilter(label='Distance', method=filter_distance) 
    location = django_filters.CharFilter(label='Location', method=filter_location) 

    class Meta: 
     model = Company 
     fields = ['distance', 'service', 'tags', 'location'] 


class CompanyListView(viewsets.ReadOnlyModelViewSet): 
    queryset = Company.objects.all() 
    filter_class = ListingFilter 

回答

1

一切都在self.data爲有序字典:

class ListingFilter(django_filters.FilterSet): 
    def filter_location(self, queryset, value): 
     print(self.data['distance']) 
     return queryset