Ticket #11707: limit_ForeignKey.2.patch
File limit_ForeignKey.2.patch, 3.1 KB (added by , 15 years ago) |
---|
-
django/db/models/fields/related.py
848 848 db = kwargs.pop('using', None) 849 849 defaults = { 850 850 'form_class': forms.ModelChoiceField, 851 'queryset': self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to) ,851 'queryset': self.rel.to._default_manager.using(db).complex_filter(self.rel.limit_choices_to).distinct(), 852 852 'to_field_name': self.rel.field_name, 853 853 } 854 854 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, Post10 from models import Foo, Bar, Baz, Whiz, BigD, BigS, Image, BigInt, Post 10 11 11 12 try: 12 13 from decimal import Decimal … … 73 74 # This should not crash. That counts as a win for our purposes. 74 75 Foo.objects.filter(d__gte=100000000000) 75 76 77 class BazForm(forms.ModelForm): 78 class Meta: 79 model = Baz 80 76 81 class ForeignKeyTests(django.test.TestCase): 77 82 def test_callable_default(self): 78 83 """Test the use of a lazy callable for ForeignKey.default""" … … 80 85 b = Bar.objects.create(b="bcd") 81 86 self.assertEqual(b.a, a) 82 87 88 def test_distinct_choice_limit(self): 89 """Doesn't make sense to offer the same ForeignKey multiple times in a form""" 90 a = Foo.objects.create(a='a', d=Decimal("-1")) 91 b = Foo.objects.create(a='b', d=Decimal("1")) 92 bar_a = Bar.objects.create(b='ah', a=a) 93 bar_b = Bar.objects.create(b='aha', a=a) 94 bar_b = Bar.objects.create(b='bla', a=b) 95 form = BazForm() 96 fk_field = str(form['foo']) 97 self.assertEqual(len(re.findall(r'value="2"', fk_field)), 0) 98 self.assertEqual(len(re.findall(r'value="1"', fk_field)), 1) 99 83 100 class DateTimeFieldTests(unittest.TestCase): 84 101 def test_datetimefield_to_python_usecs(self): 85 102 """DateTimeField.to_python should support usecs""" -
tests/regressiontests/model_fields/models.py
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', (