+1 from me. As Simon covered on the ticket, the change is small. Making
Django classes support __init_subclass__ might unlock some nice dynamic
field patterns.

On Thu, May 11, 2023 at 12:47 PM hottwaj <jonathan.a.cla...@gmail.com>
wrote:

> Hi there, I opened the above ticket and submitted a PR with fix and test
> added.  I was asked to bring the issue here for wider review before the
> ticket is re-opened (if that is what people agree to do)
>
> For reference, links to the ticket and PR are:
> https://code.djangoproject.com/ticket/34555
> https://github.com/django/django/pull/16849
>
> The issue raised is that current implementation of ModelBase.__new__
> prevents use of __init_subclass__ on a Model to add model fields
>
> e.g. the code listed at the end of this email does not currently work (the
> PR fixes this).
>
> Currently the same result could be achieved by i) writing a new metaclass
> e.g. BaseBookModelMeta or ii) using a class decorator where
> cls.add_to_class(field) is called.
>
> Using __init_subclass__ is advised as a simpler alternative to writing a
> metaclass to customise class creation, hence this PR.
>
> Hope that makes sense and appreciate any feedback.  Thanks!
>
>
> class BaseBookModel(models.Model):
>     class Meta:
>         abstract = True
>
>     def __init_subclass__(cls, author_cls, **kwargs):
>         super().__init_subclass__(**kwargs)
>         cls.author = models.ForeignKey(author_cls,
> on_delete=models.CASCADE)
>
> class Author(models.Model):
>     name = models.CharField(max_length=256, unique=True)
>
> class Book(BaseBookModel, author_cls=Author):
>     pass
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/015a5798-d084-4afb-b800-e83154301ec7n%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/015a5798-d084-4afb-b800-e83154301ec7n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CAMyDDM2OojqTe5r_SBiWcVAgOLSZHNcdwg9qvLBT%2BBOpxR2yyg%40mail.gmail.com.
  • Tic... hottwaj
    • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
      • ... charettes
        • ... hottwaj
        • ... Jonathan Clarke
          • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
            • ... Jonathan Clarke
              • ... 'Adam Johnson' via Django developers (Contributions to Django itself)

Reply via email to