Ticket #12510: 12510-against-12104.diff

File 12510-against-12104.diff, 2.7 KB (added by Honza Král, 15 years ago)

Changed tests to UnitTest, simplified them a bit.

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index ff20c93..b80415e 100644
    a b class ModelChoiceField(ChoiceField):  
    875875
    876876    choices = property(_get_choices, ChoiceField._set_choices)
    877877
    878     def clean(self, value):
    879         Field.clean(self, value)
     878    def to_python(self, value):
    880879        if value in EMPTY_VALUES:
    881880            return None
    882881        try:
    class ModelChoiceField(ChoiceField):  
    886885            raise ValidationError(self.error_messages['invalid_choice'])
    887886        return value
    888887
     888    def validate(self, value):
     889        return Field.validate(self, value)
     890
    889891class ModelMultipleChoiceField(ModelChoiceField):
    890892    """A MultipleChoiceField whose choices are a model QuerySet."""
    891893    widget = SelectMultiple
  • tests/regressiontests/forms/models.py

    diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
    index 3b47b50..de91ec6 100644
    a b import datetime  
    33import tempfile
    44import shutil
    55
    6 from django.db import models
     6from django.db import models, connection
     7from django.conf import settings
    78# Can't import as "forms" due to implementation details in the test suite (the
    89# current file is called "forms" and is already imported).
    910from django import forms as django_forms
    1011from django.core.files.storage import FileSystemStorage
     12from django.test import TestCase
    1113
    1214temp_storage_location = tempfile.mkdtemp()
    1315temp_storage = FileSystemStorage(location=temp_storage_location)
    class FileModel(models.Model):  
    4143class FileForm(django_forms.Form):
    4244    file1 = django_forms.FileField()
    4345
     46class Group(models.Model):
     47    name = models.CharField(max_length=10)
     48
     49    def __unicode__(self):
     50        return u'%s' % self.name
     51
     52class TestTicket12510(TestCase):
     53    ''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
     54    def setUp(self):
     55        self.groups = [Group.objects.create(name=name) for name in 'abc']
     56        self.old_debug = settings.DEBUG
     57        # turn debug on to get access to connection.queries
     58        settings.DEBUG = True
     59
     60    def tearDown(self):
     61        settings.DEBUG = self.old_debug
     62
     63    def test_choices_not_fetched_when_not_rendering(self):
     64        field = django_forms.ModelChoiceField(Group.objects.order_by('-name'))
     65        self.assertEqual('a', field.clean(self.groups[0].pk).name)
     66        # only one query is required to pull the model from DB
     67        self.assertEqual(1, len(connection.queries))
     68
     69
    4470__test__ = {'API_TESTS': """
    4571>>> from django.forms.models import ModelForm
    4672>>> from django.core.files.uploadedfile import SimpleUploadedFile
Back to Top