django rest framework model serializers - read nested, write flat

后端 未结 3 1113
忘了有多久
忘了有多久 2021-02-12 20:38

I have a situation where my client is attempting to write a representation that includes a list of fk\'s

{
languages: [1]
last_name: \"Beecher\"
settings: 1
stat         


        
3条回答
  •  猫巷女王i
    2021-02-12 20:54

    Thanks to previous posts, I went with a similar solution based off get_serializer_class for this.

    I also wanted to be able to change the serializer class depending on method.

    First, I added an attribute to the view class with a dictionary mapping request methods to serializer classes.

     serializer_classes = {
         'GET': NestedSerializer,
         'POST': FlatSerializer
     }
    

    Then, I defined a mixin to use where I want this behavior.

    class SwappableSerializerMixin(object):
        def get_serializer_class(self):
            try:
                return self.serializer_classes[self.request.method]
            except AttributeError:
                logger.debug('%(cls)s does not have the required serializer_classes'
                             'property' % {'cls': self.__class__.__name__})
                raise AttributeError
            except KeyError:
                logger.debug('request method %(method)s is not listed'
                             ' in %(cls)s serializer_classes' %
                             {'cls': self.__class__.__name__,
                              'method': self.request.method})
                # required if you don't include all the methods (option, etc) in your serializer_class
                return super(SwappableSerializerMixin, self).get_serializer_class() es
    

提交回复
热议问题