Ticket #14567: empty_queryset_test.diff

File empty_queryset_test.diff, 2.1 KB (added by Stephen Burrows, 14 years ago)
  • 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')
Back to Top