Mike Doudkin
Mike Doudkin

Reputation: 638

Django Rest Framework passing context to Meta

is there a way to pass serializer context to class Meta? In general, I want to include a URL to nested API views only in Browsable API, so I pass a 'renderer' value to serializer context, and want to check it in Meta to include additional fields or not to do so. Maybe there is a more elegant way?

Upvotes: 1

Views: 229

Answers (1)

Ben Graham
Ben Graham

Reputation: 2099

Override your serializer __init__ and switch out the hyperlinked field in self.fields for whatever you please (documentation example for dynamically changing fields) based on the accepted renderer on self.context['request'].

Upvotes: 1

Related Questions