Unfortunately, a direct usage of __init_subclass__ in a subclass of Django
Model class results in TypeError:
{{{
$ python manage.py shell
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/core/management/__init__.py", line 381, in
execute_from_command_line
utility.execute()
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/core/management/__init__.py", line 357, in execute
django.setup()
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/apps/registry.py", line 112, in populate
app_config.import_models()
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/apps/config.py", line 198, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in
import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in
_find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in
_call_with_frames_removed
File "/home/tt/mysite/polls/models.py", line 25, in <module>
class MyPluginModel(MyMasterModel):
File "/home/tt/django_reproducer/lib64/python3.6/site-
packages/django/db/models/base.py", line 78, in __new__
new_class = super_new(cls, name, bases, new_attrs, **kwargs)
TypeError: __init_subclass__() missing 1 required positional argument:
'cls'
}}}
To reproduce, just use the code below in models.py:
(FWIW, I created a django project and then polls app using
[https://docs.djangoproject.com/en/2.1/intro/tutorial01/ the tutorial] and
then added the code to models.py)
{{{
from django.db import models
# It works as a mixin class.
# Expected/actual result: MyModel.some_attr == 'added_by
__init_subclass__'
class MyMixin:
def __init_subclass__(cls, **kwargs):
cls.some_attr = 'added_by __init_subclass__'
super().__init_subclass__(**kwargs)
class MyModel(models.Model, MyMixin):
pass
# It doesn't work directly in the subclass of the Model class
# Expected result: MyPluginModel.some_attr == 'added_by __init_subclass__'
# Actual result: TypeError: __init_subclass__() missing 1 required
positional argument: 'cls'
class MyMasterModel(models.Model): # remove inheritance from models.Model
and it will work.
def __init_subclass__(cls, **kwargs):
cls.some_attr = 'added_by __init_subclass__'
super().__init_subclass__(**kwargs)
class MyPluginModel(MyMasterModel):
pass
}}}
OS: Fedora 28
Django: 2.1.4
Python: 3.6.6
--
Ticket URL: <https://code.djangoproject.com/ticket/30041>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* type: Uncategorized => Bug
--
Ticket URL: <https://code.djangoproject.com/ticket/30041#comment:1>
Comment (by Tatiana Tereshchenko):
It seems that explicit {{{@classmethod}}} decorator helps, though
[https://www.python.org/dev/peps/pep-0487/#requiring-an-explicit-
decorator-on-init-subclass PEP says] that requiring it is among "rejected
design options".
See [https://www.python.org/dev/peps/pep-0487/#subclass-registration
example of __init_subclass__ usage from PEP]
--
Ticket URL: <https://code.djangoproject.com/ticket/30041#comment:2>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/10763 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/30041#comment:3>
* component: Uncategorized => Database layer (models, ORM)
* stage: Unreviewed => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/30041#comment:4>
* status: new => closed
* resolution: => fixed
Comment:
In [changeset:"a68ea231012434b522ce45c513d84add516afa60" a68ea23]:
{{{
#!CommitTicketReference repository=""
revision="a68ea231012434b522ce45c513d84add516afa60"
Fixed #29970, #30041 -- Made ModelBase.__new__() pass attrs without
contribute_to_class() to type.__new__().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/30041#comment:5>