Ticket #11707: limit_ForeignKey.2.patch

File limit_ForeignKey.2.patch, 3.1 KB (added by Chris.Wesseling@…, 15 years ago)
  • django/db/models/fields/related.py

     
    848848        db = kwargs.pop('using', None)
    849849        defaults = {
    850850            '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(),
    852852            'to_field_name': self.rel.field_name,
    853853        }
    854854        defaults.update(kwargs)
  • tests/regressiontests/model_fields/tests.py

     
    11import datetime
    22import unittest
     3import re
    34
    45import django.test
    56from django import forms
    67from django.db import models
    78from django.core.exceptions import ValidationError
    89
    9 from models import Foo, Bar, Whiz, BigD, BigS, Image, BigInt, Post
     10from models import Foo, Bar, Baz, Whiz, BigD, BigS, Image, BigInt, Post
    1011
    1112try:
    1213    from decimal import Decimal
     
    7374        # This should not crash. That counts as a win for our purposes.
    7475        Foo.objects.filter(d__gte=100000000000)
    7576
     77class BazForm(forms.ModelForm):
     78    class Meta:
     79        model = Baz
     80
    7681class ForeignKeyTests(django.test.TestCase):
    7782    def test_callable_default(self):
    7883        """Test the use of a lazy callable for ForeignKey.default"""
     
    8085        b = Bar.objects.create(b="bcd")
    8186        self.assertEqual(b.a, a)
    8287
     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
    83100class DateTimeFieldTests(unittest.TestCase):
    84101    def test_datetimefield_to_python_usecs(self):
    85102        """DateTimeField.to_python should support usecs"""
  • tests/regressiontests/model_fields/models.py

     
    2929    b = models.CharField(max_length=10)
    3030    a = models.ForeignKey(Foo, default=get_foo)
    3131
     32class 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
    3237class Whiz(models.Model):
    3338    CHOICES = (
    3439        ('Group 1', (
Back to Top