Ticket #11707: limit_ForeignKey.3.patch
File limit_ForeignKey.3.patch, 3.1 KB (added by , 15 years ago) |
---|
-
django/db/models/fields/related.py
891 891 db = kwargs.pop('using', None) 892 892 defaults = { 893 893 'form_class': forms.ModelChoiceField, 894 'queryset': self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to) ,894 'queryset': self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to).distinct(), 895 895 'to_field_name': self.rel.field_name, 896 896 } 897 897 defaults.update(kwargs) -
tests/regressiontests/model_fields/tests.py
1 1 import datetime 2 2 import unittest 3 import re 3 4 4 5 import django.test 5 6 from django import forms 6 7 from django.db import models 7 8 from django.core.exceptions import ValidationError 8 9 9 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel10 from models import Foo, Bar, Baz, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel 10 11 11 12 try: 12 13 from decimal import Decimal … … 99 100 # This should not crash. That counts as a win for our purposes. 100 101 Foo.objects.filter(d__gte=100000000000) 101 102 103 class BazForm(forms.ModelForm): 104 class Meta: 105 model = Baz 106 102 107 class ForeignKeyTests(django.test.TestCase): 103 108 def test_callable_default(self): 104 109 """Test the use of a lazy callable for ForeignKey.default""" … … 106 111 b = Bar.objects.create(b="bcd") 107 112 self.assertEqual(b.a, a) 108 113 114 def test_distinct_choice_limit(self): 115 """Doesn't make sense to offer the same ForeignKey multiple times in a form""" 116 a = Foo.objects.create(a='a', d=Decimal("-1")) 117 b = Foo.objects.create(a='b', d=Decimal("1")) 118 bar_a = Bar.objects.create(b='ah', a=a) 119 bar_b = Bar.objects.create(b='aha', a=a) 120 bar_b = Bar.objects.create(b='bla', a=b) 121 form = BazForm() 122 fk_field = str(form['foo']) 123 self.assertEqual(len(re.findall(r'value="2"', fk_field)), 0) 124 self.assertEqual(len(re.findall(r'value="1"', fk_field)), 1) 125 109 126 class DateTimeFieldTests(unittest.TestCase): 110 127 def test_datetimefield_to_python_usecs(self): 111 128 """DateTimeField.to_python should support usecs""" -
tests/regressiontests/model_fields/models.py
34 34 b = models.CharField(max_length=10) 35 35 a = models.ForeignKey(Foo, default=get_foo) 36 36 37 class Baz(models.Model): 38 a = models.CharField(max_length=5) 39 #Only Foos related to Bars starting with 'a' 40 foo = models.ForeignKey(Foo, limit_choices_to=models.Q(bar__b__startswith='a')) 41 37 42 class Whiz(models.Model): 38 43 CHOICES = ( 39 44 ('Group 1', (