From 00a685178a6a8c1e2a3fe99ad20eb05d8533e175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Honza=20Kr=C3=A1l?= Date: Wed, 13 Oct 2010 04:46:33 +0000 Subject: [PATCH] Fixed #14119 -- fields_for_model no longer returns all fields when fields parameter is the empty tuple. Thanks alexdutton! git-svn-id: http://code.djangoproject.com/svn/django/trunk@14199 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/models.py | 4 +-- .../model_forms_regress/tests.py | 27 ++++++++++++++++++- .../model_formsets_regress/tests.py | 8 ++++++ 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/django/forms/models.py b/django/forms/models.py index d5d9fd1944..2f41dbf9df 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -40,7 +40,7 @@ def construct_instance(form, instance, fields=None, exclude=None): if not f.editable or isinstance(f, models.AutoField) \ or not f.name in cleaned_data: continue - if fields and f.name not in fields: + if fields is not None and f.name not in fields: continue if exclude and f.name in exclude: continue @@ -168,7 +168,7 @@ def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_c for f in opts.fields + opts.many_to_many: if not f.editable: continue - if fields and not f.name in fields: + if fields is not None and not f.name in fields: continue if exclude and f.name in exclude: continue diff --git a/tests/regressiontests/model_forms_regress/tests.py b/tests/regressiontests/model_forms_regress/tests.py index 2b201a7ab1..4a88ce4800 100644 --- a/tests/regressiontests/model_forms_regress/tests.py +++ b/tests/regressiontests/model_forms_regress/tests.py @@ -2,7 +2,7 @@ import unittest from datetime import date from django import forms -from django.forms.models import modelform_factory, ModelChoiceField +from django.forms.models import modelform_factory, ModelChoiceField, fields_for_model, construct_instance from django.test import TestCase from django.core.exceptions import FieldError, ValidationError from django.core.files.uploadedfile import SimpleUploadedFile @@ -417,3 +417,28 @@ class UniqueErrorsTests(TestCase): self.assertEquals(form.errors, {'__all__': [u'Edition with this Author and Publication already exists.']}) form = EditionForm(data={'author': self.author2.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161487777'}) self.assertEquals(form.errors, {'__all__': [u'Edition with this Publication and Edition already exists.']}) + + +class EmptyFieldsTestCase(TestCase): + "Tests for fields=() cases as reported in #14119" + class EmptyPersonForm(forms.ModelForm): + class Meta: + model = Person + fields = () + + def test_empty_fields_to_fields_for_model(self): + "An argument of fields=() to fields_for_model should return an empty dictionary" + field_dict = fields_for_model(Person, fields=()) + self.assertEqual(len(field_dict), 0) + + def test_empty_fields_on_modelform(self): + "No fields on a ModelForm should actually result in no fields" + form = self.EmptyPersonForm() + self.assertEqual(len(form.fields), 0) + + def test_empty_fields_to_construct_instance(self): + "No fields should be set on a model instance if construct_instance receives fields=()" + form = modelform_factory(Person)({'name': 'John Doe'}) + self.assertTrue(form.is_valid()) + instance = construct_instance(form, Person(), fields=()) + self.assertEqual(instance.name, '') diff --git a/tests/regressiontests/model_formsets_regress/tests.py b/tests/regressiontests/model_formsets_regress/tests.py index ee2a26f6c2..2113623883 100644 --- a/tests/regressiontests/model_formsets_regress/tests.py +++ b/tests/regressiontests/model_formsets_regress/tests.py @@ -159,6 +159,14 @@ class InlineFormsetTests(TestCase): form = Form(instance=None) formset = FormSet(instance=None) + def test_empty_fields_on_modelformset(self): + "No fields passed to modelformset_factory should result in no fields on returned forms except for the id. See #14119." + UserFormSet = modelformset_factory(User, fields=()) + formset = UserFormSet() + for form in formset.forms: + self.assertTrue('id' in form.fields) + self.assertEqual(len(form.fields), 1) + class CustomWidget(forms.CharField): pass