mirror of
https://github.com/django/django.git
synced 2025-06-05 11:39:13 +00:00
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
This commit is contained in:
parent
eb2cbb6db1
commit
223b2721aa
@ -903,8 +903,7 @@ class ModelChoiceField(ChoiceField):
|
|||||||
|
|
||||||
choices = property(_get_choices, ChoiceField._set_choices)
|
choices = property(_get_choices, ChoiceField._set_choices)
|
||||||
|
|
||||||
def clean(self, value):
|
def to_python(self, value):
|
||||||
Field.clean(self, value)
|
|
||||||
if value in EMPTY_VALUES:
|
if value in EMPTY_VALUES:
|
||||||
return None
|
return None
|
||||||
try:
|
try:
|
||||||
@ -914,6 +913,9 @@ class ModelChoiceField(ChoiceField):
|
|||||||
raise ValidationError(self.error_messages['invalid_choice'])
|
raise ValidationError(self.error_messages['invalid_choice'])
|
||||||
return value
|
return value
|
||||||
|
|
||||||
|
def validate(self, value):
|
||||||
|
return Field.validate(self, value)
|
||||||
|
|
||||||
class ModelMultipleChoiceField(ModelChoiceField):
|
class ModelMultipleChoiceField(ModelChoiceField):
|
||||||
"""A MultipleChoiceField whose choices are a model QuerySet."""
|
"""A MultipleChoiceField whose choices are a model QuerySet."""
|
||||||
widget = SelectMultiple
|
widget = SelectMultiple
|
||||||
|
@ -3,11 +3,13 @@ import datetime
|
|||||||
import tempfile
|
import tempfile
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models, connection
|
||||||
|
from django.conf import settings
|
||||||
# Can't import as "forms" due to implementation details in the test suite (the
|
# Can't import as "forms" due to implementation details in the test suite (the
|
||||||
# current file is called "forms" and is already imported).
|
# current file is called "forms" and is already imported).
|
||||||
from django import forms as django_forms
|
from django import forms as django_forms
|
||||||
from django.core.files.storage import FileSystemStorage
|
from django.core.files.storage import FileSystemStorage
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
temp_storage_location = tempfile.mkdtemp()
|
temp_storage_location = tempfile.mkdtemp()
|
||||||
temp_storage = FileSystemStorage(location=temp_storage_location)
|
temp_storage = FileSystemStorage(location=temp_storage_location)
|
||||||
@ -41,6 +43,30 @@ class FileModel(models.Model):
|
|||||||
class FileForm(django_forms.Form):
|
class FileForm(django_forms.Form):
|
||||||
file1 = django_forms.FileField()
|
file1 = django_forms.FileField()
|
||||||
|
|
||||||
|
class Group(models.Model):
|
||||||
|
name = models.CharField(max_length=10)
|
||||||
|
|
||||||
|
def __unicode__(self):
|
||||||
|
return u'%s' % self.name
|
||||||
|
|
||||||
|
class TestTicket12510(TestCase):
|
||||||
|
''' It is not necessary to generate choices for ModelChoiceField (regression test for #12510). '''
|
||||||
|
def setUp(self):
|
||||||
|
self.groups = [Group.objects.create(name=name) for name in 'abc']
|
||||||
|
self.old_debug = settings.DEBUG
|
||||||
|
# turn debug on to get access to connection.queries
|
||||||
|
settings.DEBUG = True
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
settings.DEBUG = self.old_debug
|
||||||
|
|
||||||
|
def test_choices_not_fetched_when_not_rendering(self):
|
||||||
|
field = django_forms.ModelChoiceField(Group.objects.order_by('-name'))
|
||||||
|
self.assertEqual('a', field.clean(self.groups[0].pk).name)
|
||||||
|
# only one query is required to pull the model from DB
|
||||||
|
self.assertEqual(1, len(connection.queries))
|
||||||
|
|
||||||
|
|
||||||
__test__ = {'API_TESTS': """
|
__test__ = {'API_TESTS': """
|
||||||
>>> from django.forms.models import ModelForm
|
>>> from django.forms.models import ModelForm
|
||||||
>>> from django.core.files.uploadedfile import SimpleUploadedFile
|
>>> from django.core.files.uploadedfile import SimpleUploadedFile
|
||||||
|
Loading…
x
Reference in New Issue
Block a user