Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
  • Loading branch information...
commit 6902824ac204d2207f50a3b788ae9d3d5947d01f 1 parent 470d9b2
Luke Plant authored February 21, 2011
2  django/db/models/fields/related.py
@@ -910,7 +910,7 @@ def formfield(self, **kwargs):
5  tests/regressiontests/model_fields/models.py
@@ -29,6 +29,11 @@ class Bar(models.Model):
29 29
     b = models.CharField(max_length=10)
30 30
     a = models.ForeignKey(Foo, default=get_foo)
31 31
 
  32
+class Baz(models.Model):
  33
+    a = models.CharField(max_length=5)
  34
+    #Only Foos related to Bars starting with 'a'
  35
+    foo = models.ForeignKey(Foo, limit_choices_to=models.Q(bar__b__startswith='a'))
  36
+
32 37
 class Whiz(models.Model):
33 38
     CHOICES = (
34 39
         ('Group 1', (
19  tests/regressiontests/model_fields/tests.py
... ...
@@ -1,5 +1,6 @@
1 1
 import datetime
2 2
 from decimal import Decimal
  3
+import re
3 4
 
4 5
 from django import test
5 6
 from django import forms
@@ -8,7 +9,7 @@
8 9
 from django.db.models.fields.files import FieldFile
9 10
 from django.utils import unittest
10 11
 
11  
-from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document
  12
+from models import Foo, Bar, Baz, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel, BooleanModel, Document
12 13
 
13 14
 # If PIL available, do these tests.
14 15
 if Image:
@@ -95,6 +96,10 @@ def test_lookup_really_big_value(self):
95 96
         # This should not crash. That counts as a win for our purposes.
96 97
         Foo.objects.filter(d__gte=100000000000)
97 98
 
  99
+class BazForm(forms.ModelForm):
  100
+    class Meta:
  101
+        model = Baz
  102
+
98 103
 class ForeignKeyTests(test.TestCase):
99 104
     def test_callable_default(self):
100 105
         """Test the use of a lazy callable for ForeignKey.default"""
@@ -102,6 +107,18 @@ def test_callable_default(self):
102 107
         b = Bar.objects.create(b="bcd")
103 108
         self.assertEqual(b.a, a)
104 109
 
  110
+    def test_distinct_choice_limit(self):
  111
+        """Doesn't make sense to offer the same ForeignKey multiple times in a form"""
  112
+        a = Foo.objects.create(a='a', d=Decimal("-1"))
  113
+        b = Foo.objects.create(a='b', d=Decimal("1"))
  114
+        bar_a = Bar.objects.create(b='ah', a=a)
  115
+        bar_b = Bar.objects.create(b='aha', a=a)
  116
+        bar_b = Bar.objects.create(b='bla', a=b)
  117
+        form = BazForm()
  118
+        fk_field = str(form['foo'])
  119
+        self.assertEqual(len(re.findall(r'value="2"', fk_field)), 0)
  120
+        self.assertEqual(len(re.findall(r'value="1"', fk_field)), 1)
  121
+
105 122
 class DateTimeFieldTests(unittest.TestCase):
106 123
     def test_datetimefield_to_python_usecs(self):
107 124
         """DateTimeField.to_python should support usecs"""

0 notes on commit 6902824

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