diff options
| author | Ian Foote <python@ian.feete.org> | 2015-11-06 20:18:00 +0100 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2015-11-11 00:58:53 -0500 |
| commit | 6184cb9baa8426ed8b3e1fb5d96afac809dd2a0c (patch) | |
| tree | 2987ee8fdfe449b3ff3f70209497c956cd962819 /django | |
| parent | 85a41b449bfe40307d1a9c482fc39dd1f82ba470 (diff) | |
[1.7.x] Fixed #25693 -- Prevented data loss with Prefetch and ManyToManyField.
Thanks to Jamie Matthews for finding and explaining the bug.
Backport of 4608573788c04fc047da42b4b7b48fdee8136ad3 from master
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/models/query.py | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index 22919e504e..dca6eea1ba 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -1910,6 +1910,14 @@ 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: + for related_m2m in obj._meta.get_all_related_many_to_many_objects(): + if related_m2m.get_accessor_name() == to_attr: + msg = 'to_attr={} conflicts with a field on the {} model.' + raise ValueError(msg.format(to_attr, obj.__class__.__name__)) + if single: val = vals[0] if vals else None to_attr = to_attr if as_attr else cache_name |
