How to implement a request envelope in Django Rest Framework?

Viewed 43

I'm trying to implement a request envelope in DRF. The format should be (for now):

{
  "data": {} | []
}

I could implement this via a custom parser, but then this would not be reflected in the generated OpenAPI Spec or the Browsable API. I tried building something like a "wrapper serializer":

class RequestEnvelopeSerializer(serializers.Serializer):
    data = MyStuffSerializer(source="*")

But that works only for single entities because in a list, every entity now is serialized like

[
  {
    "data": ...
  }, 
  {
    "data": ...
  }
]

I want a list of entities to be serialized as

{
  "data": [
    { ... },
    { ... }
  ]
}

What is the best way to achieve that in Django?

Thanks in advance.

0 Answers
Related