0

I want to create the JSON Rest-API, but during the creation I am getting the attribute error AttributeError: 'list' object has no attribute 'name'. In following code api.nova.server_list(self.request) return the list of instances. Now I want to make JSON data only name of instance and id. But I am getting Attribute error.

Django code for creating JSON Rest-api

    class InstanceList(django.views.generic.View):
        def get(self, request, *args, **kwargs):
              instances=api.nova.server_list(self.request)
              def serializer(m):
                  ret= {}               
                  ret['Name']= m.name
                  ret['Id']=m.id
              context= {
                  'instances': [serializer(m) for m in instances],
              }
              return HttpResponse(json.dumps(context), content_type='application/json')

**AttributeError at /admin/metering/project/**

    ([
    <Server: {
        'status': u'ACTIVE',
        'OS-EXT-STS: task_state': None,
        'addresses': {
            u'private': [
                {
                    u'OS-EXT-IPS-MAC: mac_addr': u'fa: 16: 3e: 85: 98: 6a',
                    u'version': 4,
                    u'addr': u'192.0.0.6',
                    u'OS-EXT-IPS: type': u'fixed'
                }
            ]
        },
        'name': u'test2',
        'links': [
            {
                u'href': u'http: //10.0.2.15: 8774/v2/020377f522324546b072af92862e6466/servers/7a3a2eab-7d3b-498b-bc69-ba39396ada4f',
                u'rel': u'self'
            },
            {
                u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/servers/7a3a2eab-7d3b-498b-bc69-ba39396ada4f',
                u'rel': u'bookmark'
            }
        ],
        'created': u'2015-08-03T07: 58: 09Z',
        'key_name': None,
        'image': {
            u'id': u'd983bdc9-9a82-4611-b644-86f9bf53406b',
            u'links': [
                {
                    u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/images/d983bdc9-9a82-4611-b644-86f9bf53406b',
                    u'rel': u'bookmark'
                }
            ]
        },
        'OS-DCF: diskConfig': u'AUTO',
        'image_name': u'cirros-0.3.2-x86_64-uec',
        'OS-EXT-STS: power_state': 1,
        'OS-EXT-SRV-ATTR: host': u'localhost.localdomain',
        'OS-EXT-SRV-ATTR: instance_name': u'instance-00000005',
        'tenant_id': u'020377f522324546b072af92862e6466',
        'user_id': u'f807d60e53c9454ebe7b38d10d1d1c87',
        'flavor': {
            u'id': u'42',
            u'links': [
                {
                    u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/flavors/42',
                    u'rel': u'bookmark'
                }
            ]
        },
        'OS-EXT-AZ: availability_zone': u'nova',
        'id': u'7a3a2eab-7d3b-498b-bc69-ba39396ada4f',
        'metadata': {

        }
    }>,
    <Server: {
        'status': u'ACTIVE',
        'OS-EXT-STS: task_state': None,
        'addresses': {
            u'private': [
                {
                    u'OS-EXT-IPS-MAC: mac_addr': u'fa: 16: 3e: 23: 96: d9',
                    u'version': 4,
                    u'addr': u'192.0.0.5',
                    u'OS-EXT-IPS: type': u'fixed'
                },
                {
                    u'OS-EXT-IPS-MAC: mac_addr': u'fa: 16: 3e: 23: 96: d9',
                    u'version': 4,
                    u'addr': u'172.24.4.1',
                    u'OS-EXT-IPS: type': u'floating'
                }
            ]
        },
        'name': u'test1',
        'links': [
            {
                u'href': u'http: //10.0.2.15: 8774/v2/020377f522324546b072af92862e6466/servers/1114cb76-f3df-4c60-8b12-5ad14224ffbd',
                u'rel': u'self'
            },
            {
                u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/servers/1114cb76-f3df-4c60-8b12-5ad14224ffbd',
                u'rel': u'bookmark'
            }
        ],
        'created': u'2015-08-01T12: 03: 04Z',
        'key_name': None,
        'image': {
            u'id': u'd983bdc9-9a82-4611-b644-86f9bf53406b',
            u'links': [
                {
                    u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/images/d983bdc9-9a82-4611-b644-86f9bf53406b',
                    u'rel': u'bookmark'
                }
            ]
        },
        'OS-DCF: diskConfig': u'AUTO',
        'image_name': u'cirros-0.3.2-x86_64-uec',
        'OS-EXT-STS: power_state': 1,
        'OS-EXT-SRV-ATTR: host': u'localhost.localdomain',
        'OS-EXT-SRV-ATTR: instance_name': u'instance-00000004',
        'tenant_id': u'020377f522324546b072af92862e6466',
        'user_id': u'f807d60e53c9454ebe7b38d10d1d1c87',
        'flavor': {
            u'id': u'42',
            u'links': [
                {
                    u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/flavors/42',
                    u'rel': u'bookmark'
                }
            ]
        },
        'OS-EXT-AZ: availability_zone': u'nova',
        'id': u'1114cb76-f3df-4c60-8b12-5ad14224ffbd',
        'metadata': {

        }
    }>,
    <Server: {
        'status': u'ACTIVE',
        'OS-EXT-STS: task_state': None,
        'addresses': {
            u'private': [
                {
                    u'OS-EXT-IPS-MAC: mac_addr': u'fa: 16: 3e: 98: 81: 4c',
                    u'version': 4,
                    u'addr': u'192.0.0.4',
                    u'OS-EXT-IPS: type': u'fixed'
                }
            ]
        },
        'name': u'ce-2',
        'links': [
            {
                u'href': u'http: //10.0.2.15: 8774/v2/020377f522324546b072af92862e6466/servers/8b97b82b-a9e4-4fe0-adcb-eeaaac170301',
                u'rel': u'self'
            },
            {
                u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/servers/8b97b82b-a9e4-4fe0-adcb-eeaaac170301',
                u'rel': u'bookmark'
            }
        ],
        'created': u'2015-07-20T11: 25: 44Z',
        'key_name': None,
        'image': {
            u'id': u'd983bdc9-9a82-4611-b644-86f9bf53406b',
            u'links': [
                {
                    u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/images/d983bdc9-9a82-4611-b644-86f9bf53406b',
                    u'rel': u'bookmark'
                }
            ]
        },
        'OS-DCF: diskConfig': u'AUTO',
        'image_name': u'cirros-0.3.2-x86_64-uec',
        'OS-EXT-STS: power_state': 1,
        'OS-EXT-SRV-ATTR: host': u'localhost.localdomain',
        'OS-EXT-SRV-ATTR: instance_name': u'instance-00000003',
        'tenant_id': u'020377f522324546b072af92862e6466',
        'user_id': u'f807d60e53c9454ebe7b38d10d1d1c87',
        'flavor': {
            u'id': u'42',
            u'links': [
                {
                    u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/flavors/42',
                    u'rel': u'bookmark'
                }
            ]
        },
        'OS-EXT-AZ: availability_zone': u'nova',
        'id': u'8b97b82b-a9e4-4fe0-adcb-eeaaac170301',
        'metadata': {

        }
    }>,
    <Server: {
        'status': u'ACTIVE',
        'OS-EXT-STS: task_state': None,
        'addresses': {
            u'private': [
                {
                    u'OS-EXT-IPS-MAC: mac_addr': u'fa: 16: 3e: 32: db: 57',
                    u'version': 4,
                    u'addr': u'192.0.0.3',
                    u'OS-EXT-IPS: type': u'fixed'
                }
            ]
        },
        'name': u'ce-1',
        'links': [
            {
                u'href': u'http: //10.0.2.15: 8774/v2/020377f522324546b072af92862e6466/servers/afaa50ad-8025-415b-81c4-566c8e06f388',
                u'rel': u'self'
            },
            {
                u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/servers/afaa50ad-8025-415b-81c4-566c8e06f388',
                u'rel': u'bookmark'
            }
        ],
        'created': u'2015-07-20T11: 25: 44Z',
        'key_name': None,
        'image': {
            u'id': u'd983bdc9-9a82-4611-b644-86f9bf53406b',
            u'links': [
                {
                    u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/images/d983bdc9-9a82-4611-b644-86f9bf53406b',
                    u'rel': u'bookmark'
                }
            ]
        },
        'OS-DCF: diskConfig': u'AUTO',
        'image_name': u'cirros-0.3.2-x86_64-uec',
        'OS-EXT-STS: power_state': 1,
        'OS-EXT-SRV-ATTR: host': u'localhost.localdomain',
        'OS-EXT-SRV-ATTR: instance_name': u'instance-00000002',
        'tenant_id': u'020377f522324546b072af92862e6466',
        'user_id': u'f807d60e53c9454ebe7b38d10d1d1c87',
        'flavor': {
            u'id': u'42',
            u'links': [
                {
                    u'href': u'http: //10.0.2.15: 8774/020377f522324546b072af92862e6466/flavors/42',
                    u'rel': u'bookmark'
                }
            ]
        },
        'OS-EXT-AZ: availability_zone': u'nova',
        'id': u'afaa50ad-8025-415b-81c4-566c8e06f388',
        'metadata': {

        }
    }>
],
False)
3
  • Provide a stacktrace, please. Commented Aug 5, 2015 at 6:21
  • 1
    @Neelabh Singh : Kindly check the other question you asked. stackoverflow.com/questions/31816179/…. You are trying to access the name attribute on a list which is not possible try to access it on the objects inside that list. Commented Aug 5, 2015 at 6:35
  • Why not use Django-REST-Framework Commented Aug 5, 2015 at 6:42

1 Answer 1

0

I solved my problem

I got the problem, it was due to api.nova.server_list(self.request) returns two value like ([<{server1}>,<{server2}>], False) But in my code I have written like this instances=api.nova.server_list(self.request). I was not taking care of last return value 'False'. So it was giving error.

Following code is correct,

 instances, more=api.nova.server_list(self.request)
          def serializer(m):
              ret= {} 
              ret['Name']= m.name
              ret['Id']=m.id
              return ret
          context= {
              'instances': [serializer(m) for m in instances],
          }
          return HttpResponse(json.dumps([context]), content_type='application/json')
Sign up to request clarification or add additional context in comments.

1 Comment

If you are going to make a REST API, I strongly recomment you to try Django REST Framework (DRF) or any other similar mature solution. Hand-made JSON is OK when you are just playing, but with project complication growth, things will run out of control very quick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.