#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()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.

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 <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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/0107019272d1d7a9-7ca01005-f606-457f-a35c-2151f4fd72a6-000000%40eu-central-1.amazonses.com.

Reply via email to