Issues with model Manager and inheritance.
|Reported by:||loic84||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Cc:||akaariai, LaurensBER||Triage Stage:||Accepted|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||yes|
I've found a couple of issues with the current handling of managers with inheritance.
- Weird bug that I can't explain yet: https://github.com/loic/django/compare/manager_inheritance_bug
model_inheritance_regress.tests.ModelInheritanceTest.test_abstract_base_class_m2m_relation_inheritance fails with AttributeError: 'BachelorParty' object has no attribute 'bachelorparty_ptr'.
- Managers in MTI *are* inherited, contrary to what is said in the docs.
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.
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.
Change History (4)
comment:1 Changed 2 years ago by loic84
- Has patch set
- Needs documentation unset
- Needs tests set
- Patch needs improvement set