From 8641489f4dffdeff0897d7d715d02a50ff0b23cf Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Wed, 28 Jun 2017 20:30:19 -0700 Subject: [1.11.x] Fixed #28345 -- Applied limit_choices_to during ModelForm.__init__(). field_for_model() now has an additional keyword argument, apply_limit_choices_to, allowing it to continue to be used to create form fields dynamically after ModelForm.__init__() is called. Thanks Tim Graham for the review. Backport of a1be12fe193c8f3de8a0b0820f460a302472375f from master --- tests/model_forms/tests.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'tests/model_forms') diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py index fe9a153a90..f33278f88a 100644 --- a/tests/model_forms/tests.py +++ b/tests/model_forms/tests.py @@ -19,7 +19,7 @@ from django.forms.models import ( ) from django.forms.widgets import CheckboxSelectMultiple from django.template import Context, Template -from django.test import SimpleTestCase, TestCase, skipUnlessDBFeature +from django.test import SimpleTestCase, TestCase, mock, skipUnlessDBFeature from django.utils import six from django.utils._os import upath @@ -2943,6 +2943,16 @@ class LimitChoicesToTests(TestCase): fields = fields_for_model(StumpJoke, ['has_fooled_today']) self.assertSequenceEqual(fields['has_fooled_today'].queryset, [self.threepwood]) + def test_callable_called_each_time_form_is_instantiated(self): + field = StumpJokeForm.base_fields['most_recently_fooled'] + with mock.patch.object(field, 'limit_choices_to') as today_callable_dict: + StumpJokeForm() + self.assertEqual(today_callable_dict.call_count, 1) + StumpJokeForm() + self.assertEqual(today_callable_dict.call_count, 2) + StumpJokeForm() + self.assertEqual(today_callable_dict.call_count, 3) + class FormFieldCallbackTests(SimpleTestCase): -- cgit v1.3