def __init__(self, *args, **kwargs):
    fields = kwargs.pop('fields', None)
exclude = kwargs.pop('exclude', None)
nest = kwargs.pop('nest', None)
if nest is not None:
    if nest == True:
        self.Meta.depth = 1
super(DynamicModelSerializer, self).__init__(*args, **kwargs)
if fields is not None:
    allowed = set(fields)
existing = set(self.fields.keys())
for field_name in (existing - allowed):
    self.fields.pop(field_name)
if exclude is not None:
    for field_name in exclude:
        self.fields.pop(field_name)
How to dynamically change depth in Django Rest Framework nested serializers?