gpt4 book ai didi

python - Django-Filter 并使用数组进行查询

转载 作者:太空宇宙 更新时间:2023-11-03 12:46:51 25 4
gpt4 key购买 nike

我有一个简单的问题:目前我可以这样做以从我的后端获取一个对象:

http://127.0.0.1:8000/api/v1/boats/boats?id=10
http://127.0.0.1:8000/api/v1/boats/boats?home_port=98&id=5

但我想根据 id 列表或 home_ports 列表获取一组船,我已经尝试过:

http://127.0.0.1:8000/api/v1/boats/boats?id=10,11
http://127.0.0.1:8000/api/v1/boats/boats?id_in=10,11
http://127.0.0.1:8000/api/v1/boats/boats?id=10,id=11
http://127.0.0.1:8000/api/v1/boats/boats?id=10&id=11

但是这些都不行。使用 django-filter 执行此操作的最佳方法是什么,URL 规则是如何定义的?

这是我的看法:

class BoatList(generics.ListCreateAPIView):
permission_classes = (IsOwnerOrReadOnly,)
serializer_class = BoatSerializer
queryset = Boat.objects.all()
filter_backends = (filters.DjangoFilterBackend,)
filter_fields = ('id', 'home_port',)

我标记为接受的解决方案 100% 回答了我的问题,但我最终根据我发现的另一篇使用过滤器的帖子实现了一些不同的东西:

class ListFilter(Filter):

def filter(self, qs, value):
if not value:
return qs

self.lookup_type = 'in'
values = value.split(',')
return super(ListFilter, self).filter(qs, values)

class BoatFilter(FilterSet):
ids = ListFilter(name='id')

class Meta:
model = Boat
fields = ['home_port', 'ids']


class BoatList(generics.ListCreateAPIView):
permission_classes = (IsOwnerOrReadOnly,)
serializer_class = BoatSerializer
queryset = Boat.objects.all()
filter_backends = (filters.DjangoFilterBackend,)
filter_class = BoatFilter

def perform_create(self, serializer):
serializer.save(owner=self.request.user)

最佳答案

doniyor的回答很贴切。但我猜 request 在使用它的地方将不可用。

还有另一种方法。您可以覆盖 get_queryset 方法。这可以按如下方式完成:

class BoatList(generics.ListCreateAPIView):
permission_classes = (IsOwnerOrReadOnly,)
serializer_class = BoatSerializer
filter_backends = (filters.DjangoFilterBackend,)
filter_fields = ('id', 'home_port',)

def get_queryset(self):
id_list = self.request.GET.getlist("id")
if not id_list:
return []
return Boat.objects.filter(id__in=id_list)

关于python - Django-Filter 并使用数组进行查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31227419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com