I am trying to create an api endpoint that pulls all the FeaturedProvider and Testimonial objects from the database, serialize it and return it as a new serialized object HomepageContentSerializer
I've tried multiple ways but I'm getting empty {} when I try to hit the endpoint. I'm not sure where to initialize or pass the FeaturedProvider and Testimonial objects to the HomepageContentSerializer to be serialized
serializers.py
class FeaturedProviderSerializer(serializers.ModelSerializer):
class Meta:
model = FeaturedProvider
fields = '__all__'
class TestimonialSerializer(serializers.ModelSerializer):
class Meta:
model = Testimonial
fields = '__all__'
class HomepageContentSerializer(serializers.Serializer):
providers = FeaturedProviderSerializer(many=True, read_only=True)
testimonials = TestimonialSerializer(many=True, read_only=True)
views.py
class HomepageContent(APIView):
renderer_classes = [JSONRenderer]
def get(self, request):
content = HomepageContentSerializer().data
return Response(content)
My goal is to get this representation back
{
"providers": [
{ ... },
{ ... },
{ ... },
],
"testimonials": [
{ ... },
{ ... },
{ ... },
],
}