Only first meta inner class inherited with multiple abstract base models.
|Reported by:||John Shimek||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||1.3|
|Has patch:||no||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Code shows this better:
from django.db import models class FirstMixin(models.Model): name = models.CharField(max_length=100) class Meta: abstract = True class SecondMixin(models.Model): order = models.PositiveIntegerField() class Meta: abstract = True ordering = ['order'] class Concrete(FirstMixin, SecondMixin): published = models.BooleanField()
Ordering is set on the Concrete instances:
>>> from red.models import Concrete >>> c = Concrete() >>> c._meta.ordering 
If instead concrete is defined with
class Concrete(SecondMixin, FirstMixin): published = models.BooleanField()
Notice that SecondMixin is the first class that Concrete subclasses instead of FirstMixin. This results in
>>> c = Concrete() >>> c._meta.ordering ['order']
It is very unintuitive and in my opinion incorrect. The order of which classes to inherit from shouldn't prevent inheriting the Meta inner class as well. This could cause hard to find bugs, such as why 'ordering' is not correct or why two different concrete classes have different meta settings while having the same parent classes.
The other way to address this is like so:
class Concrete(FirstMixin, SecondMixin): published = models.BooleanField() class Meta(SecondMixin.Meta): pass
This is more explicit, but would a developer think this has to be done until they ran into this issue? Also, I think this is the only way to possibly get Meta options from multiple parent classes by having class Meta(SecondMixin.Meta, FirstMixin.Meta).
Change History (4)
comment:1 Changed 5 years ago by
|Patch needs improvement:||unset|
|Status:||new → closed|