Django REST framework: Can a nested object access its parent object's details in a List View?

前端 未结 2 1202
清酒与你
清酒与你 2021-02-04 03:40

I am attempting to implement a nested resource where one of its fields depends on a value from its parent resource.

Suppose we are building a system for a company which

2条回答
  •  礼貌的吻别
    2021-02-04 04:35

    In addition to the accepted answer, if you use viewsets and want your sub-resource to be a collection (filtered by the parent document) only, you can also use the @detail_route decorator, as documented in the docs:

    from rest_framework import viewsets
    from rest_framework.decorators import detail_route
    from rest_framework.response import Response
    
    class CustomerViewSet(viewsets.ModelViewSet):
        queryset = Customer.objects.all()
        serializer_class = CustomerSummarySerializer
    
        ...
    
        @detail_route(methods=['get'])
        def rep(self, request, pk=None):
            customer = self.get_object()
            queryset = customer.pk.all()
            instances = self.filter_queryset(queryset)
            serializer = RepSummarySerializer(instances,
                    context={'request': request}, many=True)
            return Response(serializer.data)
    

    Now you can query /customers/123/rep/ and you will get a list of all Rep instances for the specified customer.

    It probably won't fully solve what you need, but for many people that don't need full nested resources it's actually enough.

提交回复
热议问题