summaryrefslogtreecommitdiff
path: root/tests/regressiontests
diff options
context:
space:
mode:
authorLuke Plant <L.Plant.98@cantab.net>2011-02-21 14:03:59 +0000
committerLuke Plant <L.Plant.98@cantab.net>2011-02-21 14:03:59 +0000
commit6902824ac204d2207f50a3b788ae9d3d5947d01f (patch)
tree3a53d3cb3b6c4052f98bf3b20b4d695ee7fa7ad8 /tests/regressiontests
parent470d9b2602fe53e8c6cbcd9287cd86048cae67d0 (diff)
Fixed #11707 - limit_choices_to on a ForeignKey can render duplicate options in formfield
Thanks to Chris Wesseling for the report and patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15607 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests')
-rw-r--r--tests/regressiontests/model_fields/models.py5
-rw-r--r--tests/regressiontests/model_fields/tests.py19
2 files changed, 23 insertions, 1 deletions
diff --git a/tests/regressiontests/model_fields/models.py b/tests/regressiontests/model_fields/models.py
index 1dc1649f13..7db1f8904a 100644
--- a/tests/regressiontests/model_fields/models.py
+++ b/tests/regressiontests/model_fields/models.py
@@ -29,6 +29,11 @@ class Bar(models.Model):
b = models.CharField(max_length=10)
a = models.ForeignKey(Foo, default=get_foo)
+class Baz(models.Model):
+ a = models.CharField(max_length=5)
+ #Only Foos related to Bars starting with 'a'
+ foo = models.ForeignKey(Foo, limit_choices_to=models.Q(bar__b__startswith='a'))
+
class Whiz(models.Model):
CHOICES = (
('Group 1', (
diff --git a/tests/regressiontests/model_fields/tests.py b/tests/regressiontests/model_fields/tests.py
index a0b45931df..d3f7199ec1 100644
--- a/tests/regressiontests/model_fields/tests.py
+++ b/tests/regressiontests/model_fields/tests.py
@@ -1,5 +1,6 @@
import datetime
from decimal import Decimal
+import re
from django import test
from django import forms
@@ -8,7 +9,7 @@ from django.db import models
from django.db.models.fields.files import FieldFile
from django.utils import unittest
-from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document
+from models import Foo, Bar, Baz, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document
# If PIL available, do these tests.
if Image:
@@ -95,6 +96,10 @@ class DecimalFieldTests(test.TestCase):
# This should not crash. That counts as a win for our purposes.
Foo.objects.filter(d__gte=100000000000)
+class BazForm(forms.ModelForm):
+ class Meta:
+ model = Baz
+
class ForeignKeyTests(test.TestCase):
def test_callable_default(self):
"""Test the use of a lazy callable for ForeignKey.default"""
@@ -102,6 +107,18 @@ class ForeignKeyTests(test.TestCase):
b = Bar.objects.create(b="bcd")
self.assertEqual(b.a, a)
+ def test_distinct_choice_limit(self):
+ """Doesn't make sense to offer the same ForeignKey multiple times in a form"""
+ a = Foo.objects.create(a='a', d=Decimal("-1"))
+ b = Foo.objects.create(a='b', d=Decimal("1"))
+ bar_a = Bar.objects.create(b='ah', a=a)
+ bar_b = Bar.objects.create(b='aha', a=a)
+ bar_b = Bar.objects.create(b='bla', a=b)
+ form = BazForm()
+ fk_field = str(form['foo'])
+ self.assertEqual(len(re.findall(r'value="2"', fk_field)), 0)
+ self.assertEqual(len(re.findall(r'value="1"', fk_field)), 1)
+
class DateTimeFieldTests(unittest.TestCase):
def test_datetimefield_to_python_usecs(self):
"""DateTimeField.to_python should support usecs"""