|
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)
|
@@ -41,6 +43,30 @@ 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
|
|
0 commit comments