'Return a representation of the field that is either expanded or collapsed.'

def to_representation(self, obj):

    should_expand = (self.field_name in self.context.get('expand', []))
    field = (self.expanded if should_expand else self.collapsed)
    if (field.source is None):
        field.bind(self.field_name, self)
        if should_expand:
            self.expanded.context['expand'] = set(field.context.get('expand', []))
    return field.to_representation(obj)
