Code

Ticket #19545: 0001-Make-sure-is_multipart-works-with-empty-formsets.patch

File 0001-Make-sure-is_multipart-works-with-empty-formsets.patch, 2.1 KB (added by charettes, 19 months ago)
  • django/forms/formsets.py

    From 50e1e7105e89911b1d6df4534f0ba6c45ccce3aa Mon Sep 17 00:00:00 2001
    From: Simon Charette <charette.s@gmail.com>
    Date: Wed, 2 Jan 2013 14:45:42 -0500
    Subject: [PATCH] Make sure `is_multipart` works with empty formsets.
    
    ---
     django/forms/formsets.py                      |    5 ++++-
     tests/regressiontests/forms/tests/formsets.py |   10 ++++++++--
     2 files changed, 12 insertions(+), 3 deletions(-)
    
    diff --git a/django/forms/formsets.py b/django/forms/formsets.py
    index a84d3bc..8156037 100644
    a b class BaseFormSet(object): 
    335335        Returns True if the formset needs to be multipart, i.e. it 
    336336        has FileInput. Otherwise, False. 
    337337        """ 
    338         return self.forms and self.forms[0].is_multipart() 
     338        if self.forms: 
     339            return self.forms[0].is_multipart() 
     340        else: 
     341            return self.empty_form.is_multipart() 
    339342 
    340343    @property 
    341344    def media(self): 
  • tests/regressiontests/forms/tests/formsets.py

    diff --git a/tests/regressiontests/forms/tests/formsets.py b/tests/regressiontests/forms/tests/formsets.py
    index 0bee972..ef6f40c 100644
    a b  
    11# -*- coding: utf-8 -*- 
    22from __future__ import unicode_literals 
    33 
    4 from django.forms import Form, CharField, IntegerField, ValidationError, DateField 
    5 from django.forms.formsets import formset_factory, BaseFormSet 
     4from django.forms import (CharField, DateField, FileField, Form, IntegerField, 
     5    ValidationError) 
     6from django.forms.formsets import BaseFormSet, formset_factory 
    67from django.forms.util import ErrorList 
    78from django.test import TestCase 
    89 
    class TestEmptyFormSet(TestCase): 
    989990                js = ('some-file.js',) 
    990991        self.assertIn('some-file.js', str(formset_factory(MediaForm, extra=0)().media)) 
    991992 
     993    def test_empty_formset_is_multipart(self): 
     994        """Make sure `is_multipart()` works with empty formset, refs #19545""" 
     995        class FileForm(Form): 
     996            file = FileField() 
     997        self.assertTrue(formset_factory(FileForm, extra=0)().is_multipart())