Ticket #14567: empty_queryset_and_tests.diff

File empty_queryset_and_tests.diff, 4.8 KB (added by Stephen Burrows, 14 years ago)
  • django/forms/models.py

     
    10341034        if self.required and not value:
    10351035            raise ValidationError(self.error_messages['required'])
    10361036        elif not self.required and not value:
    1037             return []
     1037            return self.queryset.none()
    10381038        if not isinstance(value, (list, tuple)):
    10391039            raise ValidationError(self.error_messages['list'])
    10401040        for pk in value:
  • tests/regressiontests/forms/models.py

     
    44import tempfile
    55
    66from django.db import models
     7from django.db.models.query import QuerySet
    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
     
    5556                                          default=lambda: ChoiceOptionModel.objects.filter(name='default'))
    5657    multi_choice_int = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='multi_choice_int',
    5758                                              default=lambda: [1])
     59    multi_choice_optional = models.ManyToManyField(ChoiceOptionModel, blank=True, null=True,
     60                                                   related_name='multi_choice_optional')
    5861
    5962class ChoiceFieldForm(django_forms.ModelForm):
    6063    class Meta:
     
    8588        self.assertNumQueries(1, test)
    8689
    8790class ModelFormCallableModelDefault(TestCase):
     91    def test_empty_queryset_return(self):
     92        "If a model's ManyToManyField has blank=True and is saved with no data, a queryset is returned."
     93        option = ChoiceOptionModel.objects.create(id=1, name='default')
     94        form = ChoiceFieldForm({'multi_choice_optional': '', 'choice': option.id, 'choice_int': 1,
     95                              'multi_choice': ['1'], 'multi_choice_int': [1]})
     96        self.assertEqual(form.is_valid(), True)
     97        self.assertEqual(isinstance(form.cleaned_data['multi_choice_optional'], QuerySet), True)
     98        # While we're at it, test whether a QuerySet is returned if there *is* a value.
     99        self.assertEqual(isinstance(form.cleaned_data['multi_choice'], QuerySet), True)
     100
    88101    def test_no_empty_option(self):
    89102        "If a model's ForeignKey has blank=False and a default, no empty option is created (Refs #10792)."
    90103        option = ChoiceOptionModel.objects.create(name='default')
     
    117130<option value="1" selected="selected">ChoiceOption 1</option>
    118131<option value="2">ChoiceOption 2</option>
    119132<option value="3">ChoiceOption 3</option>
    120 </select><input type="hidden" name="initial-multi_choice_int" value="1" id="initial-id_multi_choice_int_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
     133</select><input type="hidden" name="initial-multi_choice_int" value="1" id="initial-id_multi_choice_int_0" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
     134<p><label for="id_multi_choice_optional">Multi choice optional:</label> <select multiple="multiple" name="multi_choice_optional" id="id_multi_choice_optional">
     135<option value="1">ChoiceOption 1</option>
     136<option value="2">ChoiceOption 2</option>
     137<option value="3">ChoiceOption 3</option>
     138</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
    121139
    122140    def test_initial_instance_value(self):
    123141        "Initial instances for model fields may also be instances (refs #7287)"
     
    150168<option value="2" selected="selected">ChoiceOption 2</option>
    151169<option value="3" selected="selected">ChoiceOption 3</option>
    152170</select><input type="hidden" name="initial-multi_choice_int" value="2" id="initial-id_multi_choice_int_0" />
    153 <input type="hidden" name="initial-multi_choice_int" value="3" id="initial-id_multi_choice_int_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
     171<input type="hidden" name="initial-multi_choice_int" value="3" id="initial-id_multi_choice_int_1" /> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>
     172<p><label for="id_multi_choice_optional">Multi choice optional:</label> <select multiple="multiple" name="multi_choice_optional" id="id_multi_choice_optional">
     173<option value="1">ChoiceOption 1</option>
     174<option value="2">ChoiceOption 2</option>
     175<option value="3">ChoiceOption 3</option>
     176</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
    154177
    155178
    156179__test__ = {'API_TESTS': """
Back to Top