Ask Your Question
0

How to filter project name from the object returned? [closed]

asked 2015-08-24 22:24:43 -0500

sanjana gravatar image

updated 2015-08-25 04:00:44 -0500

    From this how can i access all the names of the projects ? I want only the names alt_demo , demo, service,  invisible_to_admin, admin  to be stored in a list 

       [<Tenant {u'enabled': True, u'description': None, u'name': u'alt_demo', u'id': u'63f94f0b93524abe9fbf419bac2a0daf'}>, 
<Tenant {u'enabled': True, u'description': None, u'name': u'demo', u'id': u'66d219e9f37244b2a6b0a24d55bbbbbe'}>,
<Tenant {u'enabled': True, u'description': None, u'name': u'service', u'id': u'6906db4d7e464777bb88300bbc38c8d6'}>,
<Tenant {u'enabled': True, u'description': None, u'name': u'invisible_to_admin', u'id': u'77d80df5c1444a7cae3f7add5baafb1f'}>, 
<Tenant {u'enabled': True, u'description': None, u'name': u'admin', u'id': u'c5885810a9694d68a06942d90e5ba0a0'}>]


    This is my code:
    project = forms.ChoiceField(label=_("Project"),
                                    required=True)

        def __init__(self, request, *args, **kwargs):
            super(CreateAlarmInfoAction, self).__init__(request,
                                                        *args, **kwargs)
            self.fields['project'].choices = (
                self.get_project_list_choices(request))

        def get_project_list_choices(self, request):
            project_choices = [('', _("Select a Project"))]
            for project in self._get_projects(request):
                for obj in range(len(project)):
                    project_choices.append(project[obj].id, project[obj].name)

        def _get_projects(self, request):
            projects = []
            try:
                projects = api.keystone.tenant_list(request)
            except Exception:
                msg = _('Project list cannot be retrieved.')
                exceptions.handle(request, msg)
            return projects

The append part isnt working fine.

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sanjana
close date 2015-08-25 06:13:00.517783

2 answers

Sort by ยป oldest newest most voted
0

answered 2015-08-25 06:12:25 -0500

sanjana gravatar image

updated 2015-08-25 06:27:27 -0500

 project = forms.ChoiceField(label=_("Project"),
                                required=True)

    def __init__(self, request, *args, **kwargs):
        super(CreateAction, self).__init__(request,
                                                    *args, **kwargs)
        self.fields['project'].choices = (
            self.get_project_list_choices(request))

    def get_project_list_choices(self, request):
        project_choices = [('', _("Select a Project"))]
        list_of_projects = self._get_projects(request)
        for obj in list_of_projects:
            project_choices.append((obj.id, obj.name))
        return project_choices

    def _get_projects(self, request):
        projects = []
        try:
            projects, has_more = api.keystone.tenant_list(request)
        except Exception:
            msg = _('Project list cannot be retrieved.')
            exceptions.handle(request, msg)
        return projects

I am able to display the list of projects as a dropdown menu

edit flag offensive delete link more
0

answered 2015-08-25 02:39:14 -0500

That is fairly simple, you have a list of object which have dicts in it. Try this, considering you have the objects in variable names tenant_list

project_names = []
for obj in range(len(tenant_list)):
    project_names.append(obj['tenants'][i]['name'])
edit flag offensive delete link more

Comments

I have objects in variable projects and project_choices = [] initially When i try the code you mentioned i get the following error

     project_choices.append(obj['projects'][i]['name'])
    TypeError: 'int' object has no attribute '__getitem__'
sanjana gravatar imagesanjana ( 2015-08-25 04:14:13 -0500 )edit

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2015-08-24 22:24:43 -0500

Seen: 112 times

Last updated: Aug 25 '15