import json from django.apps import apps from dcim.models import Cable out = {} out['cable_fields'] = [f.name for f in Cable._meta.get_fields()] out['cables'] = [] for c in Cable.objects.all(): t = c.terminations.all().order_by('id') out['cables'].append({ 'label': c.label, 'type': c.type, 'status': c.status, 'length': str(c.length), 'len_unit': c.length_unit, 'terms': [{'end': x.cable_end, 'type': x.termination_type.split('.')[-1], 'obj': str(x.termination)} for x in t] }) print(json.dumps(out, ensure_ascii=False, indent=1, default=str))