I am using Django Rest framework. I want to serialize multiple models and send as response. Currently I can send only one model per view(like CartView
below sends only Cart object). Following models(unrelated) can be there.
class Ship_address(models.Model):
...
class Bill_address(models.Model):
...
class Cart(models.Model):
...
class Giftwrap(models.Model):
...
I tried using DjangoRestMultiplemodels, it works ok but has some limitations. Is there any in-built way ? Can't I append to the serializer that's created in the following view ?
from rest_framework.views import APIView
class CartView(APIView):
"""
Returns the Details of the cart
"""
def get(self, request, format=None, **kwargs):
cart = get_cart(request)
serializer = CartSerializer(cart)
# Can't I append anything to serializer class like below ??
# serializer.append(anotherserialzed_object) ??
return Response(serializer.data)
I really like DRF. But this use-case(of sending multiple objects) makes me think if writing a plain old Django view will be better suited for such a requirement.
You can customize it, and it wouldn't be too weird, because this is an
APIView
(as opposed to aModelViewSet
from which a human being would expect the GET to return a single model) e.g. you can return several objects from different models in your GET response