chibisov / drf-extensions

DRF-extensions is a collection of custom extensions for Django REST Framework
http://chibisov.github.io/drf-extensions/docs
MIT License
1.47k stars 208 forks source link

SerializerAPI - Mixin for using different Serializer for browsable API #60

Open cancan101 opened 9 years ago

cancan101 commented 9 years ago

Along the lines of DetailSerializerMixin:

class SerializerAPI(object):
    """
    Mixin for using different Serializer for browsable API.
    """
    def get_serializer_class(self, *args, **kwargs):
        parent = super(SerializerAPI, self).get_serializer_class(*args, **kwargs)
        # We use the API serializer when not HEAD/OPTIONS and the render is set to API
        if (self.request.method not in ['HEAD', 'OPTIONS']
            and hasattr(self.request, 'accepted_renderer')
                and self.request.accepted_renderer.format == 'api'):
            return self.serializer_class_api
        else:
            return parent
auvipy commented 8 years ago

still valid?

chibisov commented 8 years ago

I'll add my thoughts here. I think these kind of problems should be solved by some kind of state machine. For example described declaratively: