Fixed #25693 -- Prevented data loss with Prefetch and ManyToManyField.

Thanks to Jamie Matthews for finding and explaining the bug.
This commit is contained in:
Ian Foote 2015-11-06 20:18:00 +01:00 committed by Tim Graham
parent dbbae2cead
commit 4608573788
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