我想根据查询参数更改 ViewSet 中的查询集。我看到查询参数中有一个标签列表,但是当我尝试提取它们时,我只得到最后一个标签作为字符串。而且我不知道它为什么以及如何工作。有人可以为我解释一下吗?
class RecipeViewSet(ModelViewSet):
pagination_class = PageNumberPagination
permission_classes = [IsAuthenticatedOrReadOnly, IsAuthorOrReadOnly]
def get_serializer_class(self):
if self.action in ['list', 'retrieve']:
return RecipeListSerializer
return RecipeCreateSerializer
def get_queryset(self):
queryset = Recipe.objects.all()
params = self.request.query_params
tags = params.get("tags")
print("params:")
print(params) # <QueryDict: {'page': ['1'], 'limit': ['6'], 'tags': ['breakfast', 'lunch', 'dinner']}>
print("tags:")
print(type(tags)) # <class 'str'>
print(tags) # I get only str - "dinner"
if tags:
queryset = Recipe.objects.filter(tags__slug__in=tags).distinct()
return queryset