Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit f82e64c6b2ade3e70553b3ec2743dfaa7456ddb3 1 parent 411625a
Adrian Holovaty authored October 22, 2005
5  django/core/meta/__init__.py
@@ -1360,9 +1360,10 @@ def function_get_sql_clause(opts, **kwargs):
1360 1360
 def function_get_in_bulk(opts, klass, *args, **kwargs):
1361 1361
     id_list = args and args[0] or kwargs['id_list']
1362 1362
     assert id_list != [], "get_in_bulk() cannot be passed an empty list."
1363  
-    kwargs['where'] = ["%s.id IN (%s)" % (opts.db_table, ",".join(map(str, id_list)))]
  1363
+    kwargs['where'] = ["%s.%s IN (%s)" % (opts.db_table, opts.pk.column, ",".join(['%s'] * len(id_list)))]
  1364
+    kwargs['params'] = id_list
1364 1365
     obj_list = function_get_list(opts, klass, **kwargs)
1365  
-    return dict([(o.id, o) for o in obj_list])
  1366
+    return dict([(getattr(o, opts.pk.column), o) for o in obj_list])
1366 1367
 
1367 1368
 def function_get_latest(opts, klass, does_not_exist_exception, **kwargs):
1368 1369
     kwargs['order_by'] = ('-' + opts.get_latest_by,)
4  tests/testapp/models/custom_pk.py
@@ -53,6 +53,8 @@ def __repr__(self):
53 53
 >>> fran.save()
54 54
 >>> employees.get_list(last_name__exact='Jones')
55 55
 [Dan Jones, Fran Jones]
  56
+>>> employees.get_in_bulk(['ABC123', 'XYZ456'])
  57
+{'XYZ456': Fran Jones, 'ABC123': Dan Jones}
56 58
 
57 59
 >>> b = businesses.Business(name='Sears')
58 60
 >>> b.save()
@@ -62,4 +64,6 @@ def __repr__(self):
62 64
 [Dan Jones, Fran Jones]
63 65
 >>> fran.get_business_list()
64 66
 [Sears]
  67
+>>> businesses.get_in_bulk(['Sears'])
  68
+{'Sears': Sears}
65 69
 """

0 notes on commit f82e64c

Please sign in to comment.
Something went wrong with that request. Please try again.