Opened 6 years ago

Closed 6 years ago

#28799 closed Bug (invalid)

ModelFormSet is incorrectly considered as not valid if initial form is not changed and validate_min=True

Reported by: Sergey Fedoseev Owned by: nobody
Component: Forms Version: 1.11
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Sergey Fedoseev)

I encountered this while trying to migrate from 1.10 to 1.11, ModelFormSet that was valid on 1.10 became invalid on 1.11.
Here's test case:

from django.db import models
from django.forms.models import modelformset_factory
from django.test import TestCase

class Author(models.Model):
    name = models.CharField(max_length=100)

class ModelFormsetTest(TestCase):
    def test_modelformset_validate_min_and_initial(self):
        data = {
            'form-TOTAL_FORMS': '2',
            'form-INITIAL_FORMS': '0',
            'form-0-name': 'a',
            'form-1-name': 'b',
        }

        FormSet = modelformset_factory(Author, min_num=2, exclude=(), validate_min=True)
        formset = FormSet(data=data, queryset=Author.objects.none(), initial=[{'name': 'a'}])
        self.assertTrue(formset.is_valid())
        self.assertTrue(len(formset.save()), 2)

Bisected to f5c6295797b8332134fd89e0209a18a1d1d45e0c.

Change History (4)

comment:1 by Tim Graham, 6 years ago

I think the behavior change is desired because if you save that formset (on Django 1.10 and older where it passes validation), no objects will be created since the form didn't change. That behavior seems to conflict with min_num=1.

comment:2 by Sergey Fedoseev, 6 years ago

Description: modified (diff)

I updated test to make it closer to my situation: min_num=2, validate_min=True, the first form is unchanged, the second one is filled. In this case objects are created.

comment:3 by Tim Graham, 6 years ago

There's a typo in your test: self.assertTrue(len(formset.save()), 2). This fails with assertEqual as only 1 object is created (from the second form). The first, unchanged form is ignored.

comment:4 by Sergey Fedoseev, 6 years ago

Resolution: invalid
Status: newclosed

I understood after all that problem is on my side.

Note: See TracTickets for help on using tickets.
Back to Top