diff --git a/django/forms/models.py b/django/forms/models.py
index ff20c93..b80415e 100644
a
|
b
|
class ModelChoiceField(ChoiceField):
|
875 | 875 | |
876 | 876 | choices = property(_get_choices, ChoiceField._set_choices) |
877 | 877 | |
878 | | def clean(self, value): |
879 | | Field.clean(self, value) |
| 878 | def to_python(self, value): |
880 | 879 | if value in EMPTY_VALUES: |
881 | 880 | return None |
882 | 881 | try: |
… |
… |
class ModelChoiceField(ChoiceField):
|
886 | 885 | raise ValidationError(self.error_messages['invalid_choice']) |
887 | 886 | return value |
888 | 887 | |
| 888 | def validate(self, value): |
| 889 | return Field.validate(self, value) |
| 890 | |
889 | 891 | class ModelMultipleChoiceField(ModelChoiceField): |
890 | 892 | """A MultipleChoiceField whose choices are a model QuerySet.""" |
891 | 893 | widget = SelectMultiple |
diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
index 3b47b50..de91ec6 100644
a
|
b
|
import datetime
|
3 | 3 | import tempfile |
4 | 4 | import shutil |
5 | 5 | |
6 | | from django.db import models |
| 6 | from django.db import models, connection |
| 7 | from django.conf import settings |
7 | 8 | # Can't import as "forms" due to implementation details in the test suite (the |
8 | 9 | # current file is called "forms" and is already imported). |
9 | 10 | from django import forms as django_forms |
10 | 11 | from django.core.files.storage import FileSystemStorage |
| 12 | from django.test import TestCase |
11 | 13 | |
12 | 14 | temp_storage_location = tempfile.mkdtemp() |
13 | 15 | temp_storage = FileSystemStorage(location=temp_storage_location) |
… |
… |
class FileModel(models.Model):
|
41 | 43 | class FileForm(django_forms.Form): |
42 | 44 | file1 = django_forms.FileField() |
43 | 45 | |
| 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 | |
44 | 70 | __test__ = {'API_TESTS': """ |
45 | 71 | >>> from django.forms.models import ModelForm |
46 | 72 | >>> from django.core.files.uploadedfile import SimpleUploadedFile |