#35827: Model subclass with __init_subclass__ doesn't get correct _meta instance
-------------------------------+--------------------------------------
Reporter: Ben Beecher | Owner: (none)
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 5.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Description changed by Ben Beecher:
Old description:
> 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.
New description:
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>
}}}
--
--
Ticket URL: <https://code.djangoproject.com/ticket/35827#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/0107019272d3a089-4b983eb9-7856-4c53-ad3a-41d04f98c912-000000%40eu-central-1.amazonses.com.