class ShowFieldsMixin:
def get_serializer(self, *args, **kwargs):
if getattr(self, 'fields_to_show', None) is not None:
kwargs['fields'] = self.fields_to_show
return super().get_serializer(*args, **kwargs)
return super().get_serializer(*args, **kwargs)
class TrainingDetailAPIView(ShowFieldsMixin, RetrieveAPIView):
queryset = Training.objects.all()
serializer_class = TrainingSerializer
lookup_field = 'slug'
fields_to_show = ('title', 'description')
class TrainingListAPIView(ShowFieldsMixin, ListAPIView):
# bla-bla-bla
fields_to_show = ('name', 'description', 'url', 'slug')