Model Field Order not influenced by MRO of superclasses
|Reported by:||Klaas van Schelven||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||yes|
Looking at the two definitions below,
class OneTwo(AbstractModelOne, AbstractModelTwo): pass class TwoOne(AbstractModelTwo, AbstractModelOne): pass
one (I?) would expect the order of the fields in the second definition to be the opposite of the order in the first one. (Since the base classes are referenced in the opposite order). This is, however, not the case.
Django only uses creation_counter to determine the order of the fields. Therefor the actual field order is determined by the order in which the containing abstract classes are evaluated (i.e. their python code is read). If these classes come from multiple sources (apps) the field order may end up to be pretty much random (though deterministic).
The general use case is heavy use of abstract models to mix in information from various sources into actual models. In real apps the two above models are not very likely, but being able to have control over the field order (other than placing the abstract models in a certain order) is important.
I make heavy use abstract models to be able to make my apps reusable. The above situation is an important roadblock to efficient use.
A patch with a failing test is attached.
Haven't yet been able to fix this in a non-intrusive way.
I would be very much obliged if someone can come up with a solution.
Change History (20)
comment:1 Changed 6 years ago by
|Patch needs improvement:||unset|
comment:9 Changed 6 years ago by
|Patch needs improvement:||set|
|Triage Stage:||Ready for checkin → Accepted|