Want to make API with the following structure:
/user - user data
/user/companies - list of companies of the user
/user/companies/1 - specific company
/user/companies/1/contacts - contact list company
/user/companies/1/contacts/2 - specific contact
Want to do on the ViewSet, but faced with the fact that you have to override the methods to "traverse" company_id.
serializer_class = serializers.CompanyPhotoSerializer
queryset = models.CompanyPhoto.objects.all()
permission_classes = [permissions.IsOwnerOrReadOnly]
def list(self, request, *args, **kwargs):
queryset = self.a queryset.filter(company_id=kwargs['company_id'])
serializer = self.serializer_class(queryset, many=True)
def retrieve(self, request, *args, **kwargs):
queryset = self.a queryset.filter(company_id=kwargs['company_id'])\
serializer = self.serializer_class(queryset)
So therefore I have to rewrite most of Musatov and their methods. And there's a normal view function easier to use. Tell me how this can be done more elegant? Will consider all options, including CBV and so on.
Read about lookup_url_kwarg
in Generic views, but waiting for more options.