1

In my DRF i want to map a customers/sercvices relation in the view. So one Customer can have 0 - many services inside of it

My serializer class appears to be fine as inspired by How to join two models in django-rest-framework


class CustomerSerializer(serializers.ModelSerializer):

    class Meta:

        model = Customer 
        fields = ['name' , 'phone' ,'email1' ... , 'service_id'] 


class ServiceSerializer(serializers.ModelSerializer):
    class Meta:
        model = Service 
        fields = ['service_name', 'product_name', ...]


class Customer_ServiceSerializer(serializers.ModelSerializer):

    service = ServiceSerializer(many=True, read_only=True)
    class Meta:
        model = Customer
        fields = ['name' , 'phone1' , 'email1' ... 'service']  

My Customer_ServiceSerializer class seems to be nested correctly

Im stuck on how i would show these combined models in the view as i have tried multiple things and come up stuck


class Customer_serviceListAPIView(generics.ListAPIView):
...

def get(self, request, *args, **kwargs):

Above is how i started my views but i am stuck on how to solve this problem

tried for example this Serialize multiple models in a single view / https://simpleisbetterthancomplex.com/tips/2016/06/20/django-tip-5-how-to-merge-querysets.html

but i get Customer objects arent iterable so how would i go about this

Thanks in advance

Edit


class Customer_serviceListAPIView(generics.ListAPIView):
    permission_classes = (IsAuthenticated,)
    queryset = Customer.objects.all()
    serializer_class = Customer_ServiceSerializer
    lookup_field= "name"

Need to do something in the views no? as currently as it is it will just display the customers with the service_id not the service details So i need to do something extra to make it work like that

0 Answers0