jazzband/django-model-utils

Adding async support to `InheritanceManager`/`InheritanceQuerySet`

microHoffman opened this issue · 0 comments

Problem

Hey, since Django ORM is starting to support async more and more, I'm wondering if you guys thought about adding async support to InheritanceManager (and InheritanceQuerySet respectively)?

Environment

  • Django Model Utils version: 4.2.0
  • Django version: 4.1
  • Python version: 3.10
  • Other libraries used, if any: not-relevant

Code examples

So far I was thinking of primitive solution to this problem and I've ended up with something like this, but I have not tested it yet (hopefully will get to testing it in few days). Do you think it should work, or is there some bigger problem about integrating async support to InheritanceManager?

class ExtendedInheritanceQuerySet(InheritanceQuerySet):
    """InheritanceQuerySet from django-model-utils extended for async methods."""

    async def aselect_subclasses(self, *subclasses):
        return await sync_to_async(self.select_subclasses)(*subclasses)

    async def aget_subclass(self, *args, **kwargs):
        return await sync_to_async(self.get_subclass)(*args, **kwargs)


class ExtendedInheritanceManager(InheritanceManager):
    """InheritanceManager from django-model-utils extended for async methods."""

    # todo also possible to create manager dynamically using `from_queryset` or `as_manager`

    _queryset_class = ExtendedInheritanceQuerySet

    async def aselect_subclasses(self, *subclasses):
        return await self.get_queryset().aselect_subclasses(*subclasses)

    async def aget_subclass(self, *args, **kwargs):
        return await self.get_queryset().aget_subclass(*args, **kwargs)