import json from django.apps import apps Device = apps.get_model('dcim', 'Device') Interface = apps.get_model('dcim', 'Interface') FrontPort = apps.get_model('dcim', 'FrontPort') # Панель PP.E.1/А.1 и её порты (первые 6) d = Device.objects.filter(name='PP.E.1/А.1').first() out = {'dev': d.name if d else None} if d: out['ifaces'] = [{'name': i.name, 'type': str(getattr(i,'type',None))} for i in Interface.objects.filter(device=d).order_by('name')[:6]] out['fronts'] = [{'name': p.name, 'type': str(getattr(p,'type',None)), 'label': p.label} for p in FrontPort.objects.filter(device=d).order_by('name')[:6]] # Коммутатор SWa.S.1/А.1 - его интерфейсы d2 = Device.objects.filter(name='SWa.S.1/А.1').first() if d2: out['swa_ifaces'] = [{'name': i.name, 'type': str(getattr(i,'type',None))} for i in Interface.objects.filter(device=d2).order_by('name')[:8]] print(json.dumps(out, ensure_ascii=False, indent=1, default=str))