mirror of https://github.com/django/django.git
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
This commit is contained in:
parent
52716ddd1b
commit
00a685178a
|
@ -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
|
||||
|
|
|
@ -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, '')
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue