Model subclass with __init_subclass__ doesn't get correct _meta instance
When defining and using a model mixin like so:
class ModelMixin(Model):
class Meta:
abstract = True
def __init_subclass__(cls) -> None:
super().__init_subclass__()
breakpoint()
cls._my_setup_func()
class TestModel(ModelMixin):
name = models.CharField(max_length=100)
The init_subclass call will be first called when the class is created here:
https://github.com/django/django/blob/main/django/db/models/base.py#L120
However the meta is attached here:
https://github.com/django/django/blob/main/django/db/models/base.py#L143
So during the init_subclass call you will have the superclass' meta object if you try to access it.
55 def __init_subclass__(cls) -> None:
56 super().__init_subclass__()
57 breakpoint()
58 -> cls._my_setup_func()
(Pdb++) cls
<class 'label.TestModel'>
(Pdb++) cls._meta
<Options for ModelMixin>
Change History
(9)
| Description: |
modified (diff)
|
| Description: |
modified (diff)
|
| Description: |
modified (diff)
|
| Description: |
modified (diff)
|
| Component: |
Uncategorized → Database layer (models, ORM)
|
| Triage Stage: |
Unreviewed → Someday/Maybe
|
| Type: |
Uncategorized → New feature
|
| Cc: |
Clifford Gama added
|
| Has patch: |
set
|
| Owner: |
set to CharulL00
|
| Status: |
new → assigned
|
I think this can be won't-fix on the same ground as #34555.
In order to truly support
__init_subclass__for different use case we'd need someone to own the problem space and suggest an implementation change toBaseModel.__new__that is backward compatible and receives the support of the developers on the forum like any new large feature requests.