summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/models.py6
1 files changed, 5 insertions, 1 deletions
diff --git a/django/forms/models.py b/django/forms/models.py
index 71da43390c..32f0f043ae 100644
--- a/django/forms/models.py
+++ b/django/forms/models.py
@@ -134,7 +134,11 @@ def model_to_dict(instance, fields=None, exclude=None):
data[f.name] = []
else:
# MultipleChoiceWidget needs a list of pks, not object instances.
- data[f.name] = list(f.value_from_object(instance).values_list('pk', flat=True))
+ qs = f.value_from_object(instance)
+ if qs._result_cache is not None:
+ data[f.name] = [item.pk for item in qs]
+ else:
+ data[f.name] = list(qs.values_list('pk', flat=True))
else:
data[f.name] = f.value_from_object(instance)
return data