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