Use different serializers for input and output from a service

后端 未结 2 939
小鲜肉
小鲜肉 2021-02-01 06:50

A default DRF resource is limited to accepting the same object it later returns. I want to use a different serializer for the input than the output. For example, I want to imp

2条回答
  •  悲哀的现实
    2021-02-01 07:01

    One of the solutions is TastyPie for Django, which has eg.:

    • Resource.alter_deserialized_detail_data() - allowing you to alter incoming single resource structure before interpreting it further,
    • Resource.alter_detail_data_to_serialize() - allowing you to alter outbound single resource structure before serialization,

    Similar is true when serializing / de-serializing lists: Resource.alter_deserialized_list_data() and Resource.alter_list_data_to_serialize().

    Note: But I believe something similar is (or will be) possible with Django REST Framework. DRF is relatively new, and recently faced some significant refactoring. Django REST Framework has pretty good opinion in Django community, and seemingly has insightful development team, so maybe you should think asking their developers or proposing improvements. Of course, if you won't find any help on StackOverflow (or encounter some answers from DRF developers here).

提交回复
热议问题