Dash Winterson
Dash Winterson

Reputation: 1295

Specifying one of many keys for a DRF serializer response

I'm building an API to a spec which has been defined by a third party, the response returns a list of items, each specified by a key which indicates it's type.

example:

[
  {
    'someType': {
      'name': 'some type instance',
      'someTypeSpecificField': 'foo',
    }
  },
  {
    'someOtherType': {
      'name': 'some other type instance',
      'someOtherTypeSpecificField': 123,
    }
  },
  {
    'someType': {
      ...
    }
  },
  ...
]

Where each of the items in the response are derived from one model ie.

class SomeModel(models.Model):
  name = models.CharField()
  type = models.ChoiceField()
  ...

I was curious what the best way to define this type of output might be using DRF serializers. Ideally it would be DRF-y enough to parse out properly in our AutoSchema, but any solutions are welcome.

Upvotes: 0

Views: 83

Answers (0)

Related Questions