From f82e64c6b2ade3e70553b3ec2743dfaa7456ddb3 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sat, 22 Oct 2005 21:18:53 +0000 Subject: [PATCH] Fixed #681 -- get_in_bulk no longer assumes PK fields are called id. Also added unit tests to confirm. Thanks, Jeremy Dunck git-svn-id: http://code.djangoproject.com/svn/django/trunk@991 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/meta/__init__.py | 5 +++-- tests/testapp/models/custom_pk.py | 4 ++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/django/core/meta/__init__.py b/django/core/meta/__init__.py index f934f9dd6c..2e39f72e95 100644 --- a/django/core/meta/__init__.py +++ b/django/core/meta/__init__.py @@ -1360,9 +1360,10 @@ def function_get_sql_clause(opts, **kwargs): def function_get_in_bulk(opts, klass, *args, **kwargs): id_list = args and args[0] or kwargs['id_list'] assert id_list != [], "get_in_bulk() cannot be passed an empty list." - kwargs['where'] = ["%s.id IN (%s)" % (opts.db_table, ",".join(map(str, id_list)))] + kwargs['where'] = ["%s.%s IN (%s)" % (opts.db_table, opts.pk.column, ",".join(['%s'] * len(id_list)))] + kwargs['params'] = id_list obj_list = function_get_list(opts, klass, **kwargs) - return dict([(o.id, o) for o in obj_list]) + return dict([(getattr(o, opts.pk.column), o) for o in obj_list]) def function_get_latest(opts, klass, does_not_exist_exception, **kwargs): kwargs['order_by'] = ('-' + opts.get_latest_by,) diff --git a/tests/testapp/models/custom_pk.py b/tests/testapp/models/custom_pk.py index 234b5c3308..5b0eb45462 100644 --- a/tests/testapp/models/custom_pk.py +++ b/tests/testapp/models/custom_pk.py @@ -53,6 +53,8 @@ EmployeeDoesNotExist: Employee does not exist for {'pk': 'foo'} >>> fran.save() >>> employees.get_list(last_name__exact='Jones') [Dan Jones, Fran Jones] +>>> employees.get_in_bulk(['ABC123', 'XYZ456']) +{'XYZ456': Fran Jones, 'ABC123': Dan Jones} >>> b = businesses.Business(name='Sears') >>> b.save() @@ -62,4 +64,6 @@ True [Dan Jones, Fran Jones] >>> fran.get_business_list() [Sears] +>>> businesses.get_in_bulk(['Sears']) +{'Sears': Sears} """