[Django] #30254: Custom model metaclasses cannot access the attribute dict in __init__

8 views
Skip to first unread message

Django

unread,
Mar 14, 2019, 12:08:47 PM3/14/19
to django-...@googlegroups.com
#30254: Custom model metaclasses cannot access the attribute dict in __init__
-------------------------------------+-------------------------------------
Reporter: Matt | Owner: nobody
Westcott |
Type: Bug | Status: new
Component: Database | Version: 2.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
In Django <=2.2, it is possible for models to define a custom metaclass
(as a subclass of `models.base.ModelBase`) and access the attribute dict
of the class being defined:

{{{
from django.db import models


class PageBase(models.base.ModelBase):
def __init__(cls, name, bases, dct):
super(PageBase, cls).__init__(name, bases, dct)
if 'magic' in dct:
print("enabling magic on %s" % (name))


class Page(models.Model, metaclass=PageBase):
magic = True

title = models.CharField(max_length=255)
}}}

As of commit a68ea231012434b522ce45c513d84add516afa60, this fails because
all attributes without a `contribute_to_class` method are popped from the
dict in `ModelBase.__new__` .

(This pattern is used by Wagtail's Page model
https://github.com/wagtail/wagtail/blob/3e1e67021e0a20783ed59e17b43e3c481897fce3/wagtail/core/models.py#L190
, so this is causing various failures against django stable/2.2.x.)

--
Ticket URL: <https://code.djangoproject.com/ticket/30254>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 14, 2019, 12:52:52 PM3/14/19
to django-...@googlegroups.com
#30254: Custom model metaclasses cannot access the attribute dict in __init__
-------------------------------------+-------------------------------------
Reporter: Matt Westcott | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.2
(models, ORM) |
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
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* cc: Sergey Fedoseev (added)


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

Django

unread,
Mar 14, 2019, 1:47:39 PM3/14/19
to django-...@googlegroups.com
#30254: Custom model metaclasses cannot access the attribute dict in __init__
-------------------------------------+-------------------------------------
Reporter: Matt Westcott | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Matt Westcott):

* has_patch: 0 => 1


Comment:

PR: https://github.com/django/django/pull/11085

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

Django

unread,
Mar 14, 2019, 2:42:15 PM3/14/19
to django-...@googlegroups.com
#30254: Custom model metaclasses cannot access the attribute dict in __init__
-------------------------------------+-------------------------------------
Reporter: Matt Westcott | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted


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

Django

unread,
Mar 14, 2019, 9:02:50 PM3/14/19
to django-...@googlegroups.com
#30254: Custom model metaclasses cannot access the attribute dict in __init__
-------------------------------------+-------------------------------------
Reporter: Matt Westcott | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* severity: Normal => Release blocker
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/30254#comment:4>

Django

unread,
Mar 14, 2019, 9:31:35 PM3/14/19
to django-...@googlegroups.com
#30254: Custom model metaclasses cannot access the attribute dict in __init__
-------------------------------------+-------------------------------------
Reporter: Matt Westcott | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham <timograham@…>):

* status: new => closed
* resolution: => fixed


Comment:

In [changeset:"985e6c224be0681d7a753b9ded4646024d332bc7" 985e6c22]:
{{{
#!CommitTicketReference repository=""
revision="985e6c224be0681d7a753b9ded4646024d332bc7"
[2.2.x] Fixed #30254 -- Allowed model metaclasses to access the attribute
dict in __init__().

Regression in a68ea231012434b522ce45c513d84add516afa60.

Backport of 58ad030d05fa50cfed327368ab61defca3303e02 from master.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30254#comment:5>

Django

unread,
Mar 14, 2019, 9:31:36 PM3/14/19
to django-...@googlegroups.com
#30254: Custom model metaclasses cannot access the attribute dict in __init__
-------------------------------------+-------------------------------------
Reporter: Matt Westcott | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham <timograham@…>):

In [changeset:"58ad030d05fa50cfed327368ab61defca3303e02" 58ad030d]:
{{{
#!CommitTicketReference repository=""
revision="58ad030d05fa50cfed327368ab61defca3303e02"
Fixed #30254 -- Allowed model metaclasses to access the attribute dict in
__init__().

Regression in a68ea231012434b522ce45c513d84add516afa60.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/30254#comment:6>

Django

unread,
Apr 24, 2019, 4:00:06 AM4/24/19
to django-...@googlegroups.com
#30254: Custom model metaclasses cannot access the attribute dict in __init__
-------------------------------------+-------------------------------------
Reporter: Matt Westcott | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 2.2
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Carlton Gibson <carlton.gibson@…>):

In [changeset:"607ff4efa31cd0c2217ed021dc939ffddad89c97" 607ff4e]:
{{{
#!CommitTicketReference repository=""
revision="607ff4efa31cd0c2217ed021dc939ffddad89c97"
Refs #30254 -- Added tests for Model.__hash__() inheritance.
}}}

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

Reply all
Reply to author
Forward
0 new messages