Ticket #14567: empty_queryset_and_tests_r14864.diff

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

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

     
    11# -*- coding: utf-8 -*-
    22import datetime
    33from django.core.files.uploadedfile import SimpleUploadedFile
     4from django.db.models.query import QuerySet
    45from django.forms import Form, ModelForm, FileField, ModelChoiceField
    56from django.test import TestCase
    67from regressiontests.forms.models import ChoiceModel, ChoiceOptionModel, ChoiceFieldModel, FileModel, Group, BoundaryModel, Defaults
     
    2829        self.assertNumQueries(1, test)
    2930
    3031class ModelFormCallableModelDefault(TestCase):
     32    def test_empty_queryset_return(self):
     33        "If a model's ManyToManyField has blank=True and is saved with no data, a queryset is returned."
     34        option = ChoiceOptionModel.objects.create(id=1, name='default')
     35        form = ChoiceFieldForm({'multi_choice_optional': '', 'choice': option.id, 'choice_int': 1,
     36                              'multi_choice': ['1'], 'multi_choice_int': [1]})
     37        self.assertEqual(form.is_valid(), True)
     38        self.assertEqual(isinstance(form.cleaned_data['multi_choice_optional'], QuerySet), True)
     39        # While we're at it, test whether a QuerySet is returned if there *is* a value.
     40        self.assertEqual(isinstance(form.cleaned_data['multi_choice'], QuerySet), True)
     41
    3142    def test_no_empty_option(self):
    3243        "If a model's ForeignKey has blank=False and a default, no empty option is created (Refs #10792)."
    3344        option = ChoiceOptionModel.objects.create(name='default')
     
    6071<option value="1" selected="selected">ChoiceOption 1</option>
    6172<option value="2">ChoiceOption 2</option>
    6273<option value="3">ChoiceOption 3</option>
    63 </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>""")
     74</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>
     75<p><label for="id_multi_choice_optional">Multi choice optional:</label> <select multiple="multiple" name="multi_choice_optional" id="id_multi_choice_optional">
     76<option value="1">ChoiceOption 1</option>
     77<option value="2">ChoiceOption 2</option>
     78<option value="3">ChoiceOption 3</option>
     79</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
    6480
    6581    def test_initial_instance_value(self):
    6682        "Initial instances for model fields may also be instances (refs #7287)"
     
    93109<option value="2" selected="selected">ChoiceOption 2</option>
    94110<option value="3" selected="selected">ChoiceOption 3</option>
    95111</select><input type="hidden" name="initial-multi_choice_int" value="2" id="initial-id_multi_choice_int_0" />
    96 <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>""")
     112<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>
     113<p><label for="id_multi_choice_optional">Multi choice optional:</label> <select multiple="multiple" name="multi_choice_optional" id="id_multi_choice_optional">
     114<option value="1">ChoiceOption 1</option>
     115<option value="2">ChoiceOption 2</option>
     116<option value="3">ChoiceOption 3</option>
     117</select> <span class="helptext"> Hold down "Control", or "Command" on a Mac, to select more than one.</span></p>""")
    97118
    98119
    99120
  • tests/regressiontests/forms/models.py

     
    5757                                          default=lambda: ChoiceOptionModel.objects.filter(name='default'))
    5858    multi_choice_int = models.ManyToManyField(ChoiceOptionModel, blank=False, related_name='multi_choice_int',
    5959                                              default=lambda: [1])
     60    multi_choice_optional = models.ManyToManyField(ChoiceOptionModel, blank=True, null=True,
     61                                                   related_name='multi_choice_optional')
    6062
    6163
    6264class FileModel(models.Model):
  • docs/ref/forms/fields.txt

     
    929929.. class:: ModelMultipleChoiceField(**kwargs)
    930930
    931931    * Default widget: ``SelectMultiple``
    932     * Empty value: ``[]`` (an empty list)
    933     * Normalizes to: A list of model instances.
     932    * Empty value: An empty ``QuerySet`` (self.queryset.none())
     933    * Normalizes to: A ``QuerySet`` of model instances.
    934934    * Validates that every id in the given list of values exists in the
    935935      queryset.
    936936    * Error message keys: ``required``, ``list``, ``invalid_choice``,
Back to Top