From 4d279b8baf4a42be9a7b167df48e58acf52e4f41 Mon Sep 17 00:00:00 2001 From: Zhigang Wang Date: Sat, 22 Jun 2013 11:26:41 -0400 Subject: [PATCH] Make get_object_by_id() work for most Django objects. Django object id is usually an int while lookup is an unicode string. It works for Openstack but not other Django objects. This patch makes other standard Django apps reusing Horizon easier. Change-Id: I436c34e8b17340537f8d201001741d34c933764e --- horizon/tables/base.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/horizon/tables/base.py b/horizon/tables/base.py index bc1933b44..71b39e43a 100644 --- a/horizon/tables/base.py +++ b/horizon/tables/base.py @@ -1029,10 +1029,20 @@ class DataTable(object): the ``lookup`` parameter specified. An error will be raised if the match is not a single data object. + We will convert the object id and ``lookup`` to unicode before + comparison. + Uses :meth:`~horizon.tables.DataTable.get_object_id` internally. """ - matches = [datum for datum in self.data if - self.get_object_id(datum) == lookup] + if not isinstance(lookup, unicode): + lookup = unicode(str(lookup), 'utf-8') + matches = [] + for datum in self.data: + obj_id = self.get_object_id(datum) + if not isinstance(obj_id, unicode): + obj_id = unicode(str(obj_id), 'utf-8') + if obj_id == lookup: + matches.append(datum) if len(matches) > 1: raise ValueError("Multiple matches were returned for that id: %s." % matches)