diff options
| author | Ian Foote <python@ian.feete.org> | 2015-11-06 20:18:00 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-11-10 12:12:45 -0500 |
| commit | f9a08eb8975d8a49bee8cc42ae207100adb6ce75 (patch) | |
| tree | 7d0a561ebccdedb819236273ea1d82a1f0b6d0a1 /django | |
| parent | 3dcdfcc8d359fa15f62c46e16875082c8a9c28ff (diff) | |
[1.9.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 | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/django/db/models/query.py b/django/db/models/query.py index b7d55b6a4b..63d3ea7530 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -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 |
