Ticket #14119: fix-14119.diff

File fix-14119.diff, 3.7 KB (added by alexdutton, 14 years ago)

Patch and regression tests

  • django/forms/models.py

     
    3939        if not f.editable or isinstance(f, models.AutoField) \
    4040                or not f.name in cleaned_data:
    4141            continue
    42         if fields and f.name not in fields:
     42        if fields is not None and f.name not in fields:
    4343            continue
    4444        if exclude and f.name in exclude:
    4545            continue
     
    167167    for f in opts.fields + opts.many_to_many:
    168168        if not f.editable:
    169169            continue
    170         if fields and not f.name in fields:
     170        if fields is not None and not f.name in fields:
    171171            continue
    172172        if exclude and f.name in exclude:
    173173            continue
  • tests/regressiontests/model_forms_regress/tests.py

     
    22
    33from django import db
    44from django import forms
    5 from django.forms.models import modelform_factory, ModelChoiceField
     5from django.forms.models import modelform_factory, ModelChoiceField, fields_for_model, construct_instance
    66from django.conf import settings
    77from django.test import TestCase
    88
     
    250250        form.is_valid()
    251251        # self.assertTrue(form.is_valid())
    252252        # self.assertEquals(form.cleaned_data['url'], 'http://example.com/test')
     253
     254class EmptyFieldsTestCase(TestCase):
     255    "Tests for fields=() cases as reported in #14119"
     256    class EmptyPersonForm(forms.ModelForm):
     257        class Meta:
     258            model = Person
     259            fields = ()
     260
     261    def test_empty_fields_to_fields_for_model(self):
     262        "An argument of fields=() to fields_for_model should return an empty dictionary"
     263        field_dict = fields_for_model(Person, fields=())
     264        self.assertEqual(len(field_dict), 0)
     265
     266    def test_empty_fields_on_modelform(self):
     267        "No fields on a ModelForm should actually result in no fields"
     268        form = self.EmptyPersonForm()
     269        self.assertEqual(len(form.fields), 0)
     270
     271    def test_empty_fields_to_construct_instance(self):
     272        "No fields should be set on a model instance if construct_instance receives fields=()"
     273        form = modelform_factory(Person)({'name': 'John Doe'})
     274        self.assertTrue(form.is_valid())
     275        instance = construct_instance(form, Person(), fields=())
     276        self.assertEqual(instance.name, '')
  • tests/regressiontests/model_formsets_regress/tests.py

     
    1 from django.forms.models import modelform_factory, inlineformset_factory
     1from django.forms.models import modelform_factory, modelformset_factory, inlineformset_factory
    22from django.test import TestCase
    33
    44from models import User, UserSite, Restaurant, Manager
     
    156156        # you can create a formset with an instance of None
    157157        form = Form(instance=None)
    158158        formset = FormSet(instance=None)
     159
     160    def test_empty_fields_on_modelformset(self):
     161        "No fields passed to modelformset_factory should result in no fields on returned forms except for the id. See #14119."
     162        UserFormSet = modelformset_factory(User, fields=())
     163        formset = UserFormSet()
     164        for form in formset.forms:
     165            self.assertTrue('id' in form.fields)
     166            self.assertEqual(len(form.fields), 1)
Back to Top