from rest_framework import viewsets

from paranuara_api.models import Company, Person
from paranuara_api.serializers import (
        CompanySerializer, CompanyListSerializer, PersonListSerializer,
        PersonSerializer
)

class MultiSerializerMixin(object):

    def get_serializer_class(self):
        return self.serializers[self.action]


class CompanyViewSet(MultiSerializerMixin, viewsets.ReadOnlyModelViewSet):
    queryset = Company.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': CompanyListSerializer,
        'retrieve': CompanySerializer,
    }


class PersonViewSet(MultiSerializerMixin, viewsets.ReadOnlyModelViewSet):
    queryset = Person.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': PersonListSerializer,
        'retrieve': PersonSerializer,
    }


from rest_framework import viewsets

from paranuara_api.models import Company, Person
from paranuara_api.serializers import (
        CompanySerializer, CompanyListSerializer, PersonListSerializer,
        PersonSerializer
)

class CompanyViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Company.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': CompanyListSerializer,
        'retrieve': CompanySerializer,
    }

    def get_serializer_class(self):
        return self.serializers[self.action]


class PersonViewSet(viewsets.ReadOnlyModelViewSet):
    queryset = Person.objects.all()
    lookup_field = 'index'

    serializers = {
        'list': PersonListSerializer,
        'retrieve': PersonSerializer,
    }

    def get_serializer_class(self):
        return self.serializers[self.action]


--- 
+++ 
@@ -6,7 +6,13 @@
         PersonSerializer
 )
 
-class CompanyViewSet(viewsets.ReadOnlyModelViewSet):
+class MultiSerializerMixin(object):
+
+    def get_serializer_class(self):
+        return self.serializers[self.action]
+
+
+class CompanyViewSet(MultiSerializerMixin, viewsets.ReadOnlyModelViewSet):
     queryset = Company.objects.all()
     lookup_field = 'index'
 
@@ -15,11 +21,8 @@
         'retrieve': CompanySerializer,
     }
 
-    def get_serializer_class(self):
-        return self.serializers[self.action]
 
-
-class PersonViewSet(viewsets.ReadOnlyModelViewSet):
+class PersonViewSet(MultiSerializerMixin, viewsets.ReadOnlyModelViewSet):
     queryset = Person.objects.all()
     lookup_field = 'index'
 
@@ -28,6 +31,3 @@
         'retrieve': PersonSerializer,
     }
 
-    def get_serializer_class(self):
-        return self.serializers[self.action]
-
Refactor common serializer selection code.
