summaryrefslogtreecommitdiff
path: root/tests/model_forms/tests.py
diff options
context:
space:
mode:
authorChristopher Adams <christopher.r.adams@gmail.com>2014-02-01 14:23:31 -0500
committerTim Graham <timograham@gmail.com>2014-02-11 14:05:12 -0500
commiteefc88feefec0c3685bfb102714530b751b4ae90 (patch)
tree3cab4b02c13b76b6355d475d91ca2e157a126b18 /tests/model_forms/tests.py
parenta718fcf201b04ba254e9073be82f51ae1ae3a853 (diff)
Fixed #2445 -- Allowed limit_choices_to attribute to be a callable.
ForeignKey or ManyToManyField attribute ``limit_choices_to`` can now be a callable that returns either a ``Q`` object or a dict. Thanks michael at actrix.gen.nz for the original suggestion.
Diffstat (limited to 'tests/model_forms/tests.py')
-rw-r--r--tests/model_forms/tests.py40
1 files changed, 39 insertions, 1 deletions
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index c64bad5241..a0507a1e1c 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -22,7 +22,8 @@ from .models import (Article, ArticleStatus, BetterWriter, BigInt, Book,
DerivedPost, ExplicitPK, FlexibleDatePost, ImprovedArticle,
ImprovedArticleWithParentLink, Inventory, Post, Price,
Product, TextFile, Writer, WriterProfile, Colour, ColourfulItem,
- ArticleStatusNote, DateTimePost, CustomErrorMessage, test_images)
+ ArticleStatusNote, DateTimePost, CustomErrorMessage, test_images,
+ StumpJoke, Character)
if test_images:
from .models import ImageFile, OptionalImageFile
@@ -521,6 +522,12 @@ class FieldOverridesTroughFormMetaForm(forms.ModelForm):
}
+class StumpJokeForm(forms.ModelForm):
+ class Meta:
+ model = StumpJoke
+ fields = '__all__'
+
+
class TestFieldOverridesTroughFormMeta(TestCase):
def test_widget_overrides(self):
form = FieldOverridesTroughFormMetaForm()
@@ -1921,3 +1928,34 @@ class ModelFormInheritanceTests(TestCase):
self.assertEqual(list(type(str('NewForm'), (ModelForm, Mixin, Form), {})().fields.keys()), ['name'])
self.assertEqual(list(type(str('NewForm'), (ModelForm, Form, Mixin), {})().fields.keys()), ['name', 'age'])
self.assertEqual(list(type(str('NewForm'), (ModelForm, Form), {'age': None})().fields.keys()), ['name'])
+
+
+class LimitChoicesToTest(TestCase):
+ """
+ Tests the functionality of ``limit_choices_to``.
+ """
+ def setUp(self):
+ self.threepwood = Character.objects.create(
+ username='threepwood',
+ last_action=datetime.datetime.today() + datetime.timedelta(days=1),
+ )
+ self.marley = Character.objects.create(
+ username='marley',
+ last_action=datetime.datetime.today() - datetime.timedelta(days=1),
+ )
+
+ def test_limit_choices_to_callable_for_fk_rel(self):
+ """
+ A ForeignKey relation can use ``limit_choices_to`` as a callable, re #2554.
+ """
+ stumpjokeform = StumpJokeForm()
+ self.assertIn(self.threepwood, stumpjokeform.fields['most_recently_fooled'].queryset)
+ self.assertNotIn(self.marley, stumpjokeform.fields['most_recently_fooled'].queryset)
+
+ def test_limit_choices_to_callable_for_m2m_rel(self):
+ """
+ A ManyToMany relation can use ``limit_choices_to`` as a callable, re #2554.
+ """
+ stumpjokeform = StumpJokeForm()
+ self.assertIn(self.threepwood, stumpjokeform.fields['has_fooled_today'].queryset)
+ self.assertNotIn(self.marley, stumpjokeform.fields['has_fooled_today'].queryset)