Opened 2 years ago

Closed 2 years ago

#30041 closed Bug (fixed)

Can't use __init_subclass__ in the Model subclass directly (not as a mixin class)

Reported by: Tatiana Tereshchenko Owned by: nobody
Component: Database layer (models, ORM) Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


There is a closed Django ticket which references PEP and describes the new __init_subclass__ in Python 3.6, it solves the case when __init_subclass__ is used in a mixin class.

Unfortunately, a direct usage of init_subclass in a subclass of Django Model class results in TypeError:

$ python shell
Traceback (most recent call last):
  File "", line 15, in <module>
  File "/home/tt/django_reproducer/lib64/python3.6/site-packages/django/core/management/", line 381, in execute_from_command_line
  File "/home/tt/django_reproducer/lib64/python3.6/site-packages/django/core/management/", line 357, in execute
  File "/home/tt/django_reproducer/lib64/python3.6/site-packages/django/", line 24, in setup
  File "/home/tt/django_reproducer/lib64/python3.6/site-packages/django/apps/", line 112, in populate
  File "/home/tt/django_reproducer/lib64/python3.6/site-packages/django/apps/", line 198, in import_models
    self.models_module = import_module(models_module_name)
  File "/usr/lib64/python3.6/importlib/", 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/", line 25, in <module>
    class MyPluginModel(MyMasterModel):
  File "/home/tt/django_reproducer/lib64/python3.6/site-packages/django/db/models/", 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
(FWIW, I created a django project and then polls app using the tutorial and then added the code to

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__'

class MyModel(models.Model, MyMixin):

# 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__'

class MyPluginModel(MyMasterModel):

OS: Fedora 28
Django: 2.1.4
Python: 3.6.6

Change History (5)

comment:1 Changed 2 years ago by Tatiana Tereshchenko

Type: UncategorizedBug

comment:2 Changed 2 years ago by Tatiana Tereshchenko

It seems that explicit @classmethod decorator helps, though PEP says that requiring it is among "rejected design options".
See example of __init_subclass__ usage from PEP

comment:3 Changed 2 years ago by Mariusz Felisiak

Has patch: set

comment:4 Changed 2 years ago by Tim Graham

Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedReady for checkin

comment:5 Changed 2 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: newclosed

In a68ea23:

Fixed #29970, #30041 -- Made pass attrs without contribute_to_class() to

Note: See TracTickets for help on using tickets.
Back to Top