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

8
  • Why do you think you need to do anything in the view? The serializer should just handle it. Commented Jul 15, 2019 at 14:25
  • Added an Edit at the botom Commented Jul 15, 2019 at 14:35
  • But you've done the "something extra", which is to define Customer_ServiceSerializer with service = ServiceSerializer. Commented Jul 15, 2019 at 15:29
  • Wouldnt i need that step to join the serializer classes but i cant map that structure in the views Commented Jul 15, 2019 at 15:43
  • I do not understand what you are saying. What step? You are already joining the serializer classes. Commented Jul 15, 2019 at 15:45

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.