'How can I pass two queryset in same function using Django Rest Framework

def getProducts(request):
    products = Product.objects.all()
    p = Product.objects.all().filter(category = 1)
    serializer = ProductSerializer((products,p), many=True)
    return Response(serializer.data)**

I want to pass p and products value in the serializer object. Can I do this here?



Solution 1:[1]

with | you can union querysets. it merges two querysets together.

so you can do it:

union_products = products | p  
serializer = ProductSerializer(union_products, many=True)

Note: this doesn't work on querysets from two different models.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1