Skip to content

Commit 223b272

Browse files
committed
Fixed #12510. Changed ModelChoiceField to stop using some of its superclasses implementation. This could cause more than one query when generating choices. Thanks, Petr Marhoun and Honza Kral.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@12211 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent eb2cbb6 commit 223b272

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

django/forms/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -903,8 +903,7 @@ def _get_choices(self):
903903

904904
choices = property(_get_choices, ChoiceField._set_choices)
905905

906-
def clean(self, value):
907-
Field.clean(self, value)
906+
def to_python(self, value):
908907
if value in EMPTY_VALUES:
909908
return None
910909
try:
@@ -914,6 +913,9 @@ def clean(self, value):
914913
raise ValidationError(self.error_messages['invalid_choice'])
915914
return value
916915

916+
def validate(self, value):
917+
return Field.validate(self, value)
918+
917919
class ModelMultipleChoiceField(ModelChoiceField):
918920
"""A MultipleChoiceField whose choices are a model QuerySet."""
919921
widget = SelectMultiple

tests/regressiontests/forms/models.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import tempfile
44
import shutil
55

6-
from django.db import models
6+
from django.db import models, connection
7+
from 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).
910
from django import forms as django_forms
1011
from django.core.files.storage import FileSystemStorage
12+
from django.test import TestCase
1113

1214
temp_storage_location = tempfile.mkdtemp()
1315
temp_storage = FileSystemStorage(location=temp_storage_location)
@@ -41,6 +43,30 @@ class FileModel(models.Model):
4143
class FileForm(django_forms.Form):
4244
file1 = django_forms.FileField()
4345

46+
class Group(models.Model):
47+
name = models.CharField(max_length=10)
48+
49+
def __unicode__(self):
50+
return u'%s' % self.name
51+
52+
class 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

0 commit comments

Comments
 (0)