1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

[1.9.x] Fixed #25693 -- Prevented data loss with Prefetch and ManyToManyField.

Thanks to Jamie Matthews for finding and explaining the bug.

Backport of 4608573788 from master
This commit is contained in:
Ian Foote
2015-11-06 20:18:00 +01:00
committed by Tim Graham
parent 3dcdfcc8d3
commit f9a08eb897
5 changed files with 39 additions and 0 deletions

View File

@@ -1575,6 +1575,17 @@ def prefetch_one_level(instances, prefetcher, lookup, level):
instance_attr_val = instance_attr(obj)
vals = rel_obj_cache.get(instance_attr_val, [])
to_attr, as_attr = lookup.get_current_to_attr(level)
# Check we are not shadowing a field on obj.
if as_attr:
try:
field = obj._meta.get_field(to_attr)
except exceptions.FieldDoesNotExist:
pass
else:
msg = 'to_attr={} conflicts with a field on the {} model.'
raise ValueError(msg.format(to_attr, field.model.__name__))
if single:
val = vals[0] if vals else None
to_attr = to_attr if as_attr else cache_name