Opened 2 months ago
Last modified 2 months ago
#35827 new New feature
Model subclass with __init_subclass__ doesn't get correct _meta instance — at Version 1
Reported by: | Ben Beecher | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 5.0 |
Severity: | Normal | Keywords: | |
Cc: | Carlton Gibson, Clifford Gama | Triage Stage: | Someday/Maybe |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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()https://code.djangoproject.com/ticket/35827# 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.
Note:
See TracTickets
for help on using tickets.