summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbelegnar <admin@zilantkon.ru>2019-04-03 09:05:15 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-04-03 09:05:15 +0200
commit1ffddfc233e2d5139cc6ec31a4ec6ef70b10f87f (patch)
treef2af8085c62461668e73e48c52d2d828a07ed41c
parent714cf468e10ccbfca6a97095939563a52b99e2eb (diff)
Fixed #30302 -- Fixed forms.model_to_dict() result if empty list of fields is passed.
-rw-r--r--django/forms/models.py2
-rw-r--r--tests/model_forms/tests.py1
2 files changed, 2 insertions, 1 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index d157c291ef..5edbbd376f 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -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):
if not getattr(f, 'editable', False):
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
diff --git a/tests/model_forms/tests.py b/tests/model_forms/tests.py
index 495fba1fb2..e4f24c31ee 100644
--- a/tests/model_forms/tests.py
+++ b/tests/model_forms/tests.py
@@ -1814,6 +1814,7 @@ class ModelOneToOneFieldTests(TestCase):
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, fields=[])), [])
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'])