Given 2 main models: TransportOrder and Company with a many to many relationship through TransportOrderConsumer. I would like to achieve the following.
TransportOrder fields are listed as well as a list of hyperlinks to consumers.
I've accomplished this through the below relationship on the Company and serializer
consumers = models.ManyToManyField(Company, through = "TransportOrderConsumer")
model = TransportOrder
# fields = ('id', 'producer_system_code', 'status','producer', 'created_at')
fields = (
# read_only_fields = ('updated_at')
You would use get_serializer_class to switch on the request type for what serializer you want to use.
from rest_framework import viewsets class MyViewSet(viewsets.ModelViewSet): queryset = MyModel.objects.all() def get_serializer_class(self): """ :return: serializer class based on the request type """ if self.action == 'retrieve': return ps.MyModelDetailSerializer elif self.action == 'list': return ps.MyModelListSerializer elif self.action == 'create': return ps.MyModelCreateSerializer else: return ps.MyModelUpdateSerializer