Ticket #11707: limit_ForeignKey.3.patch

File limit_ForeignKey.3.patch, 3.1 KB (added by Chris.Wesseling@…, 15 years ago)

update resolving conflict

  • django/db/models/fields/related.py

     
    891891        db = kwargs.pop('using', None)
    892892        defaults = {
    893893            '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(),
    895895            'to_field_name': self.rel.field_name,
    896896        }
    897897        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, NullBooleanModel
     10from models import Foo, Bar, Baz, Whiz, BigD, BigS, Image, BigInt, Post, NullBooleanModel
    1011
    1112try:
    1213    from decimal import Decimal
     
    99100        # This should not crash. That counts as a win for our purposes.
    100101        Foo.objects.filter(d__gte=100000000000)
    101102
     103class BazForm(forms.ModelForm):
     104    class Meta:
     105        model = Baz
     106
    102107class ForeignKeyTests(django.test.TestCase):
    103108    def test_callable_default(self):
    104109        """Test the use of a lazy callable for ForeignKey.default"""
     
    106111        b = Bar.objects.create(b="bcd")
    107112        self.assertEqual(b.a, a)
    108113
     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
    109126class DateTimeFieldTests(unittest.TestCase):
    110127    def test_datetimefield_to_python_usecs(self):
    111128        """DateTimeField.to_python should support usecs"""
  • tests/regressiontests/model_fields/models.py

     
    3434    b = models.CharField(max_length=10)
    3535    a = models.ForeignKey(Foo, default=get_foo)
    3636
     37class 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
    3742class Whiz(models.Model):
    3843    CHOICES = (
    3944        ('Group 1', (
Back to Top