#Best way to include meta object in the response

5 messages · Page 1 of 1 (latest)

dim mauve
#

Hi,

What is the best way to include a meta object in the response like in the following example:

{
  "meta": {
    "success": true,
    "info": [
      {
        "type": "message, warning or error",
        "display": true,
        "title": "To much to dos",
        "content": "You have 100 to dos, limit reached"
      }
    ]
  },
  "data": [
    {
      "title": "to do 1"
    },
    {
      "title": "to do 100"
    }
  ]
}

I already implemented a custom Response, where I can pass my meta info object as arguments but the problem is that this data structure gets not recognized by drf-spectacular to generate the OpenAPI response schema.

Thank You for your help

marble fern
#

I think in that case, you’d have to research more on how to use serializers to do this because I think that’s one of the roles of serializers: handles how data is displayed

#

Not really sure actually 😅

#

But I’ll research more on it and get back to you

untold relic
#

I think you'll have to create your own serializer base class that inherits from the serializer class. In that base class, you can modify the to_representation method to return the meta data. I think drf-spectacular should catch it like that.