Fixed #30302 -- Fixed forms.model_to_dict() result if empty list of fields is passed.

This commit is contained in:
belegnar 2019-04-03 09:05:15 +02:00 committed by Mariusz Felisiak
parent 714cf468e1
commit 1ffddfc233
No known key found for this signature in database
GPG Key ID: 2EF56372BA48CD1B
2 changed files with 2 additions and 1 deletions

View File

@ -83,7 +83,7 @@ def model_to_dict(instance, fields=None, exclude=None):
for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many): for f in chain(opts.concrete_fields, opts.private_fields, opts.many_to_many):
if not getattr(f, 'editable', False): if not getattr(f, 'editable', False):
continue continue
if fields and f.name not in fields: if fields is not None and f.name not in fields:
continue continue
if exclude and f.name in exclude: if exclude and f.name in exclude:
continue continue

View File

@ -1814,6 +1814,7 @@ class ModelOneToOneFieldTests(TestCase):
bw = BetterWriter.objects.create(name='Joe Better', score=10) bw = BetterWriter.objects.create(name='Joe Better', score=10)
self.assertEqual(sorted(model_to_dict(bw)), ['id', 'name', 'score', 'writer_ptr']) self.assertEqual(sorted(model_to_dict(bw)), ['id', 'name', 'score', 'writer_ptr'])
self.assertEqual(sorted(model_to_dict(bw, fields=[])), [])
self.assertEqual(sorted(model_to_dict(bw, fields=['id', 'name'])), ['id', 'name']) self.assertEqual(sorted(model_to_dict(bw, fields=['id', 'name'])), ['id', 'name'])
self.assertEqual(sorted(model_to_dict(bw, exclude=[])), ['id', 'name', 'score', 'writer_ptr']) self.assertEqual(sorted(model_to_dict(bw, exclude=[])), ['id', 'name', 'score', 'writer_ptr'])
self.assertEqual(sorted(model_to_dict(bw, exclude=['id', 'name'])), ['score', 'writer_ptr']) self.assertEqual(sorted(model_to_dict(bw, exclude=['id', 'name'])), ['score', 'writer_ptr'])