Changes between Initial Version and Version 7 of Ticket #20932
- Timestamp:
- Feb 10, 2016, 11:30:49 AM (9 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #20932
- Property Has patch set
- Property Patch needs improvement set
- Property Needs tests set
- Property Triage Stage Unreviewed → Accepted
- Property Cc added
-
Ticket #20932 – Description
initial v7 1 1 I've found a couple of issues with the current handling of managers with inheritance. 2 2 3 1. Weird bug that I can't explain yet: https://github.com/loic/django/compare/manager_inheritance_bug3 1. Given: 4 4 5 `model_inheritance_regress.tests.ModelInheritanceTest.test_abstract_base_class_m2m_relation_inheritance` fails with `AttributeError: 'BachelorParty' object has no attribute 'bachelorparty_ptr'`. 5 {{{#!python 6 class AbstractEvent(models.Model): 7 events = models.Manager() 8 9 class Meta: 10 abstract = True 6 11 7 2. Managers in MTI *are* inherited, contrary to what is said [https://docs.djangoproject.com/en/dev/topics/db/managers/#custom-managers-and-model-inheritance in the docs]. 12 class BachelorParty(AbstractEvent): 13 objects = models.Manager() 8 14 9 Quoting the docs: "Managers defined on non-abstract base classes are not inherited by child classes. [...] Therefore, they aren’t passed onto child classes.". The problem is that, since we create the new class by calling `object.__new__()`, normal Python attribute resolution applies, and we gain access to the attributes of the base classes. This doesn't happen to fields because these are removed during the class creation process, but managers are left behind. It's always tempting to think we could just delete them, but you cannot delete something that is not in your own `__dict__`. The problem is not so much that we inherit those managers, but that they don't return the right model type as demonstrated in https://github.com/loic/django/compare/manager_inheritance_bug2. 15 class MessyBachelorParty(BachelorParty): 16 pass 17 }}} 10 18 11 Note: I know we usually should have one ticket for each bug. This time though, we only have a couple of lines of codes that have multiple consequences, so I'd like to keep the discussion centralized. We can always open a ticket for each bug when the time comes to actually implement what we decide. 19 If `AbstractEvent` has a manager called anything but `objects` (e.g. `events`): 20 `MessyBachelorParty.objects.model == <class 'model_inheritance_regress.models.BachelorParty'>` 21 22 Which causes the following failure: 23 {{{ 24 model_inheritance_regress.tests.ModelInheritanceTest.test_abstract_base_class_m2m_relation_inheritance` fails with `AttributeError: 'BachelorParty' object has no attribute 'bachelorparty_ptr'. 25 }}} 26 27 If `AbstractEvent` doesn't have an explicit manager, or has one called `objects`: 28 `MessyBachelorParty.objects.model == <class 'model_inheritance_regress.models.MessyBachelorParty'>` which is the expected result. 29 30 2. Managers in MTI *are* inherited, contrary to what is said [https://docs.djangoproject.com/en/dev/topics/db/managers/#custom-managers-and-model-inheritance in the docs] (now tracked specifically in #25897): 31 32 "Managers defined on non-abstract base classes are not inherited by child classes. [...] Therefore, they aren’t passed onto child classes.". 33 34 The problem is that, since we create the new class by calling `object.__new__()`, normal Python attribute resolution applies, and we gain access to the attributes of the base classes. This doesn't happen to fields because these are removed during the class creation process, but managers are left behind. It's always tempting to think we could just delete them, but you cannot delete something that is not in your own `__dict__`. The problem is not so much that we inherit those managers, but that they don't return the right model type as demonstrated in https://github.com/loic/django/compare/manager_inheritance_bug2.