#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. -- 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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/049.1b706ef25170dce224009a00d4bb45a9%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.