Ticket #14567: empty_queryset_and_tests_r16322.diff

File empty_queryset_and_tests_r16322.diff, 5.9 KB (added by Stephen Burrows, 13 years ago)
  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index ecfdc92..88da1c3 100644
    a b class ModelMultipleChoiceField(ModelChoiceField):  
    998998        if self.required and not value:
    999999            raise ValidationError(self.error_messages['required'])
    10001000        elif not self.required and not value:
    1001             return []
     1001            return self.queryset.none()
    10021002        if not isinstance(value, (list, tuple)):
    10031003            raise ValidationError(self.error_messages['list'])
    10041004        key = self.to_field_name or 'pk'
  • docs/ref/forms/fields.txt

    diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
    index 96f1be9..36db856 100644
    a b example::  
    931931.. class:: ModelMultipleChoiceField(**kwargs)
    932932
    933933    * Default widget: ``SelectMultiple``
    934     * Empty value: ``[]`` (an empty list)
    935     * Normalizes to: A list of model instances.
     934    * Empty value: An empty ``QuerySet`` (self.queryset.none())
     935    * Normalizes to: A ``QuerySet`` of model instances.
    936936    * Validates that every id in the given list of values exists in the
    937937      queryset.
    938938    * Error message keys: ``required``, ``list``, ``invalid_choice``,
  • tests/regressiontests/forms/models.py

    diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
    index 203980c..8e7bdb3 100644
    a b class ChoiceFieldModel(models.Model):  
    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):
  • tests/regressiontests/forms/tests/models.py

    diff --git a/tests/regressiontests/forms/tests/models.py b/tests/regressiontests/forms/tests/models.py
    index 3f548df..8be569a 100644
    a b  
    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
    class TestTicket12510(TestCase):  
    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')
    class ModelFormCallableModelDefault(TestCase):  
    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)"
    class ModelFormCallableModelDefault(TestCase):  
    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
Back to Top