Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #14119 -- fields_for_model no longer returns all fields when fi…

…elds parameter is the empty tuple. Thanks alexdutton!

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14199 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 00a685178a6a8c1e2a3fe99ad20eb05d8533e175 1 parent 52716dd
@HonzaKral HonzaKral authored
View
4 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
View
27 tests/regressiontests/model_forms_regress/tests.py
@@ -2,7 +2,7 @@
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 @@ def test_unique_together_error_message(self):
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, '')
View
8 tests/regressiontests/model_formsets_regress/tests.py
@@ -159,6 +159,14 @@ def test_formset_with_none_instance(self):
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
Please sign in to comment.
Something went wrong with that request. Please try again.