[Django] #35827: Model subclass with __init_subclass__ doesn't get correct _meta instance

15 views
Skip to first unread message

Django

unread,
Oct 9, 2024, 3:44:52 PMOct 9
to django-...@googlegroups.com
#35827: Model subclass with __init_subclass__ doesn't get correct _meta instance
-----------------------------+-----------------------------------------
Reporter: Ben Beecher | Type: Uncategorized
Status: new | Component: Uncategorized
Version: 5.0 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+-----------------------------------------
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.
--
Ticket URL: <https://code.djangoproject.com/ticket/35827>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Oct 9, 2024, 3:45:21 PMOct 9
to django-...@googlegroups.com
#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()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.

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35827#comment:1>

Django

unread,
Oct 9, 2024, 3:45:53 PMOct 9
to django-...@googlegroups.com
#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()
So during the init_subclass call you will have the superclass' meta object
if you try to access it.

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35827#comment:2>

Django

unread,
Oct 9, 2024, 3:46:52 PMOct 9
to django-...@googlegroups.com
> 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.

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35827#comment:3>

Django

unread,
Oct 9, 2024, 3:48:49 PMOct 9
to django-...@googlegroups.com
#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()
{{{

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

unread,
Oct 9, 2024, 8:31:30 PMOct 9
to django-...@googlegroups.com
#35827: Model subclass with __init_subclass__ doesn't get correct _meta instance
-------------------------------------+-------------------------------------
Reporter: Ben Beecher | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Someday/Maybe
* type: Uncategorized => New feature

Comment:

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
to `BaseModel.__new__` that is backward compatible and
[https://docs.djangoproject.com/en/5.1/internals/contributing/bugs-and-
features/#reporting-bugs-and-requesting-features receives the support of
the developers on the forum like any new large feature requests].
--
Ticket URL: <https://code.djangoproject.com/ticket/35827#comment:5>

Django

unread,
Oct 11, 2024, 4:11:35 AMOct 11
to django-...@googlegroups.com
#35827: Model subclass with __init_subclass__ doesn't get correct _meta instance
-------------------------------------+-------------------------------------
Reporter: Ben Beecher | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Clifford Gama):

#27880 by replacing `Options.contribute_to_class()` with
`Options.__set_name__()` allows `_meta` to be added when the model class
is created. If that gets added then `__init_subclass__()` will be
supported.
--
Ticket URL: <https://code.djangoproject.com/ticket/35827#comment:6>

Django

unread,
Oct 19, 2024, 2:40:49 AMOct 19
to django-...@googlegroups.com
#35827: Model subclass with __init_subclass__ doesn't get correct _meta instance
-------------------------------------+-------------------------------------
Reporter: Ben Beecher | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Carlton Gibson):

* cc: Carlton Gibson (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/35827#comment:7>

Django

unread,
Oct 21, 2024, 9:48:36 AMOct 21
to django-...@googlegroups.com
#35827: Model subclass with __init_subclass__ doesn't get correct _meta instance
-------------------------------------+-------------------------------------
Reporter: Ben Beecher | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Someday/Maybe
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Clifford Gama):

* cc: Clifford Gama (added)
* has_patch: 0 => 1

Comment:

Thought I should link [https://github.com/django/django/pull/18697 this
new PR] on #27880 as it would address this ticket and #34555
--
Ticket URL: <https://code.djangoproject.com/ticket/35827#comment:8>
Reply all
Reply to author
Forward
0 new messages