mirror of
https://github.com/django/django.git
synced 2025-05-05 22:47:32 +00:00
Refs #24227 -- Removed ManyToManyField special casing in model_to_dict().
This commit is contained in:
parent
8f6a1a1551
commit
67d984413c
@ -1565,7 +1565,10 @@ class ManyToManyField(RelatedField):
|
|||||||
"""
|
"""
|
||||||
Return the value of this field in the given model instance.
|
Return the value of this field in the given model instance.
|
||||||
"""
|
"""
|
||||||
return getattr(obj, self.attname).all()
|
qs = getattr(obj, self.attname).all()
|
||||||
|
if qs._result_cache is not None:
|
||||||
|
return [item.pk for item in qs]
|
||||||
|
return list(qs.values_list('pk', flat=True))
|
||||||
|
|
||||||
def save_form_data(self, instance, data):
|
def save_form_data(self, instance, data):
|
||||||
getattr(instance, self.attname).set(data)
|
getattr(instance, self.attname).set(data)
|
||||||
|
@ -88,21 +88,7 @@ def model_to_dict(instance, fields=None, exclude=None):
|
|||||||
continue
|
continue
|
||||||
if exclude and f.name in exclude:
|
if exclude and f.name in exclude:
|
||||||
continue
|
continue
|
||||||
if f.many_to_many:
|
data[f.name] = f.value_from_object(instance)
|
||||||
# If the object doesn't have a primary key yet, just use an empty
|
|
||||||
# list for its m2m fields. Calling f.value_from_object will raise
|
|
||||||
# an exception.
|
|
||||||
if instance.pk is None:
|
|
||||||
data[f.name] = []
|
|
||||||
else:
|
|
||||||
# MultipleChoiceWidget needs a list of pks, not object instances.
|
|
||||||
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
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user