Ticket #18508: django-forms-bug.patch

File django-forms-bug.patch, 3.9 KB (added by Ole Laursen, 12 years ago)

Test case and simple fix

  • django/forms/models.py

    diff --git a/django/forms/models.py b/django/forms/models.py
    index 26f8691..42f46a7 100644
    a b class BaseModelFormSet(BaseFormSet):  
    447447            if isinstance(pk, list):
    448448                pk = pk[0]
    449449            kwargs['instance'] = self._existing_object(pk)
    450         if i < self.initial_form_count() and not kwargs.get('instance'):
     450        elif i < self.initial_form_count() and not kwargs.get('instance'):
    451451            kwargs['instance'] = self.get_queryset()[i]
    452452        if i >= self.initial_form_count() and self.initial_extra:
    453453            # Set initial values for extra forms
  • tests/regressiontests/forms/models.py

    diff --git a/tests/regressiontests/forms/models.py b/tests/regressiontests/forms/models.py
    index 18e6ddc..b1151e6 100644
    a b class Group(models.Model):  
    7575
    7676class Cheese(models.Model):
    7777   name = models.CharField(max_length=100)
     78
     79
     80class DirectoryModel(models.Model):
     81    name = models.CharField(max_length=100)
     82
     83class EntryModel(models.Model):
     84    name = models.CharField(max_length=100)
     85    directory = models.ForeignKey(DirectoryModel)
  • tests/regressiontests/forms/tests/__init__.py

    diff --git a/tests/regressiontests/forms/tests/__init__.py b/tests/regressiontests/forms/tests/__init__.py
    index 8e2150c..ce43669 100644
    a b from .input_formats import (LocalizedTimeTests, CustomTimeInputFormatsTests,  
    1313    CustomDateTimeInputFormatsTests, SimpleDateTimeFormatTests)
    1414from .media import FormsMediaTestCase, StaticFormsMediaTestCase
    1515from .models import (TestTicket12510, ModelFormCallableModelDefault,
    16     FormsModelTestCase, RelatedModelFormTests)
     16    FormsModelTestCase, RelatedModelFormTests, FormsModelFormsetTestCase)
    1717from .regressions import FormsRegressionsTestCase
    1818from .util import FormsUtilTestCase
    1919from .validators import TestFieldWithValidators
  • tests/regressiontests/forms/tests/models.py

    diff --git a/tests/regressiontests/forms/tests/models.py b/tests/regressiontests/forms/tests/models.py
    index 5bea49b..9b063ce 100644
    a b import datetime  
    66from django.core.files.uploadedfile import SimpleUploadedFile
    77from django.db import models
    88from django.forms import Form, ModelForm, FileField, ModelChoiceField
    9 from django.forms.models import ModelFormMetaclass
     9from django.forms.models import ModelFormMetaclass, inlineformset_factory
    1010from django.test import TestCase
    1111
    1212from ..models import (ChoiceOptionModel, ChoiceFieldModel, FileModel, Group,
    13     BoundaryModel, Defaults)
     13    BoundaryModel, Defaults, DirectoryModel, EntryModel)
    1414
    1515
    1616class ChoiceFieldForm(ModelForm):
    class RelatedModelFormTests(TestCase):  
    196196            model=A
    197197
    198198        self.assertTrue(issubclass(ModelFormMetaclass(b'Form', (ModelForm,), {'Meta': Meta}), ModelForm))
     199
     200class EntryModelForm(ModelForm):
     201    class Meta:
     202        model = EntryModel
     203
     204EntryModelFormset = inlineformset_factory(DirectoryModel, EntryModel, EntryModelForm)
     205
     206class FormsModelFormsetTestCase(TestCase):
     207    def test_model_formset_outdated_deletion(self):
     208        d = DirectoryModel.objects.create(name="dir")
     209        e = EntryModel.objects.create(name="foo", directory=d)
     210
     211        # we only have id=1, id=2 doesn't exist
     212        formset = EntryModelFormset(data={'form-INITIAL_FORMS':'2', 'form-TOTAL_FORMS':'2', 'form-0-id':'1', 'form-0-name':'bah', 'form-1-id':'2', 'form-1-name':'baz', 'form-1-DELETE': 'on', }, prefix="form", instance=d)
     213
     214        # id=1 is good, id=2 is gone but that doesn't matter as we're deleting it
     215        self.assertTrue(formset.is_valid())
     216
     217        formset.save()
     218
     219        self.assertEquals(EntryModel.objects.get(pk=e.pk).name, "bah")
     220        # should have ignored id=2
     221        self.assertEquals(d.entrymodel_set.count(), 1)
     222        self.assertEquals(EntryModel.objects.filter(pk=2).count(), 0)
Back to Top