[英]filter_fields in django rest framework viewset are ignored
I am trying to get some basic equality filtering for my view and if I understand the documentation , I only need the filter_fields()
field defined.我正在尝试为我的视图获取一些基本的相等性过滤,如果我理解文档,我只需要定义
filter_fields()
字段。
Well, they seem to be ignored ( /api/organizations?ticker=AMZN
lists everything instead of filtering down to a single record):好吧,它们似乎被忽略了(
/api/organizations?ticker=AMZN
列出了所有内容而不是过滤到单个记录):
class OrganizationViewSet(viewsets.ModelViewSet):
queryset = Organization.objects.all()
serializer_class = OrganizationSerializer
pagination_class = CustomPagination
filter_fields = ('sector', 'industry', 'marketplace')
@staticmethod
def pack_persons_to_url(request, data):
data["persons"] = request.build_absolute_uri("/api/persons/%s/" % data["symbol"])
def list(self, request, *args, **kwargs):
response = super(OrganizationViewSet, self).list(request, *args, **kwargs)
for element in response.data["results"]:
self.pack_persons_to_url(request, element)
return response
def retrieve(self, request, *args, **kwargs):
response = super(OrganizationViewSet, self).retrieve(request, *args, **kwargs)
self.pack_persons_to_url(request, response.data)
return response
The first three fields are FKs and the ticker
is a CharField
.前三个字段是 FK,
ticker
是CharField
。 What do I need to fix to make it all work right?我需要修复什么才能使其正常工作?
I was experiencing this after upgrading to django-filters
22.1.升级到
django-filters
22.1 后我遇到了这个问题。 It seems filter_fields
was renamed to filterset_fields
.似乎
filter_fields
已重命名为filterset_fields
。
您需要将DjangoFilterBackend
添加到DjangoFilterBackend
的过滤器后端
filter_backends = (backends.DjangoFilterBackend, )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.