From e3e0cf8a0f75ae18bc401182bfbdea2c27bb0244 Mon Sep 17 00:00:00 2001 From: Collin Anderson Date: Tue, 6 Aug 2013 12:38:31 -0400 Subject: [PATCH] [1.6.x] Fixed #20865 -- Fixed raw_id_fields to work with callable limit_choices_to. Backport of d53e574676 from master --- django/contrib/admin/widgets.py | 2 ++ tests/admin_widgets/tests.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index eeae846320d..c4b15cdd6ac 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -116,6 +116,8 @@ def url_params_from_lookup_dict(lookups): if lookups and hasattr(lookups, 'items'): items = [] for k, v in lookups.items(): + if callable(v): + v = v() if isinstance(v, (tuple, list)): v = ','.join([str(x) for x in v]) elif isinstance(v, bool): diff --git a/tests/admin_widgets/tests.py b/tests/admin_widgets/tests.py index b4d6da034bb..d275c7669e9 100644 --- a/tests/admin_widgets/tests.py +++ b/tests/admin_widgets/tests.py @@ -226,6 +226,13 @@ class AdminForeignKeyRawIdWidget(DjangoTestCase): self.assertEqual(lookup1, {'color__in': 'red,blue'}) self.assertEqual(lookup1, lookup2) + def test_url_params_from_lookup_dict_callable(self): + def my_callable(): + return 'works' + lookup1 = widgets.url_params_from_lookup_dict({'myfield': my_callable}) + lookup2 = widgets.url_params_from_lookup_dict({'myfield': my_callable()}) + self.assertEqual(lookup1, lookup2) + class FilteredSelectMultipleWidgetTest(DjangoTestCase): def test_render(self):