Skip to content

Commit 00a6851

Browse files
committed
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
1 parent 52716dd commit 00a6851

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

django/forms/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def construct_instance(form, instance, fields=None, exclude=None):
4040
if not f.editable or isinstance(f, models.AutoField) \
4141
or not f.name in cleaned_data:
4242
continue
43-
if fields and f.name not in fields:
43+
if fields is not None and f.name not in fields:
4444
continue
4545
if exclude and f.name in exclude:
4646
continue
@@ -168,7 +168,7 @@ def fields_for_model(model, fields=None, exclude=None, widgets=None, formfield_c
168168
for f in opts.fields + opts.many_to_many:
169169
if not f.editable:
170170
continue
171-
if fields and not f.name in fields:
171+
if fields is not None and not f.name in fields:
172172
continue
173173
if exclude and f.name in exclude:
174174
continue

tests/regressiontests/model_forms_regress/tests.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from datetime import date
33

44
from django import forms
5-
from django.forms.models import modelform_factory, ModelChoiceField
5+
from django.forms.models import modelform_factory, ModelChoiceField, fields_for_model, construct_instance
66
from django.test import TestCase
77
from django.core.exceptions import FieldError, ValidationError
88
from django.core.files.uploadedfile import SimpleUploadedFile
@@ -417,3 +417,28 @@ def test_unique_together_error_message(self):
417417
self.assertEquals(form.errors, {'__all__': [u'Edition with this Author and Publication already exists.']})
418418
form = EditionForm(data={'author': self.author2.pk, 'publication': self.pub1.pk, 'edition': 1, 'isbn': '9783161487777'})
419419
self.assertEquals(form.errors, {'__all__': [u'Edition with this Publication and Edition already exists.']})
420+
421+
422+
class EmptyFieldsTestCase(TestCase):
423+
"Tests for fields=() cases as reported in #14119"
424+
class EmptyPersonForm(forms.ModelForm):
425+
class Meta:
426+
model = Person
427+
fields = ()
428+
429+
def test_empty_fields_to_fields_for_model(self):
430+
"An argument of fields=() to fields_for_model should return an empty dictionary"
431+
field_dict = fields_for_model(Person, fields=())
432+
self.assertEqual(len(field_dict), 0)
433+
434+
def test_empty_fields_on_modelform(self):
435+
"No fields on a ModelForm should actually result in no fields"
436+
form = self.EmptyPersonForm()
437+
self.assertEqual(len(form.fields), 0)
438+
439+
def test_empty_fields_to_construct_instance(self):
440+
"No fields should be set on a model instance if construct_instance receives fields=()"
441+
form = modelform_factory(Person)({'name': 'John Doe'})
442+
self.assertTrue(form.is_valid())
443+
instance = construct_instance(form, Person(), fields=())
444+
self.assertEqual(instance.name, '')

tests/regressiontests/model_formsets_regress/tests.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ def test_formset_with_none_instance(self):
159159
form = Form(instance=None)
160160
formset = FormSet(instance=None)
161161

162+
def test_empty_fields_on_modelformset(self):
163+
"No fields passed to modelformset_factory should result in no fields on returned forms except for the id. See #14119."
164+
UserFormSet = modelformset_factory(User, fields=())
165+
formset = UserFormSet()
166+
for form in formset.forms:
167+
self.assertTrue('id' in form.fields)
168+
self.assertEqual(len(form.fields), 1)
169+
162170

163171
class CustomWidget(forms.CharField):
164172
pass

0 commit comments

Comments
 (0)