Skip to content

fix: django async class based views#96

Merged
maldoinc merged 6 commits intomaldoinc:masterfrom
MuriloScarpaSitonio:fix-django-async-fbv
Dec 21, 2025
Merged

fix: django async class based views#96
maldoinc merged 6 commits intomaldoinc:masterfrom
MuriloScarpaSitonio:fix-django-async-fbv

Conversation

@MuriloScarpaSitonio
Copy link
Contributor

@MuriloScarpaSitonio MuriloScarpaSitonio commented Dec 18, 2025

Fix async class-based views under ASGI

Problem

Async class-based views failed when running under an ASGI server or when tested with AsyncClient.

Changes

  • Fix CBV injection to work with async containers
  • Add AsyncClient info box to Django docs
  • Add tests for async CBVs

@MuriloScarpaSitonio
Copy link
Contributor Author

MuriloScarpaSitonio commented Dec 18, 2025 via email

@maldoinc
Copy link
Owner

maldoinc commented Dec 18, 2025

So the reason async views sort of need an async container is due to the fact that you may need to create async objects. This would fail even with the proposed change since the sync container cannot create any async objects. I think it would be good to understand what prompted django to make the get_response be synchronous.

Repro to cause this to err:

class AsyncGreeterService:
    async def agreet(self, name: str) -> str:
        return f"hi {name} async"

@service
def make_async_greeter() -> AsyncGreeterService:
    return AsyncGreeterService()

Then have this be injected instead.

@maldoinc
Copy link
Owner

Looks like to get async views to work as expected in tests you need to use the django async client. Once that's done async def views even those with dependencies coming from async factories will work as expected.

@MuriloScarpaSitonio
Copy link
Contributor Author

@maldoinc pardon my stupidity I totally forgot I should use AsyncClient. That indeed fixed the main issue on my project but I think I caught another bug for async class-based views: https://github.com/maldoinc/wireup/pull/96/files#diff-2fc77cc118280f487325a4ef267a4852cd5649e5e84f6fd2903d92032ec7d355R140

@MuriloScarpaSitonio MuriloScarpaSitonio changed the title fix: django async function based views fix: django async class based views Dec 21, 2025
@maldoinc
Copy link
Owner

maldoinc commented Dec 21, 2025

Good spot. Would be good to update the PR title, body and rebase against latest master since this is quite a bit behind. Happy to merge once that's done.

Might be worth mentioning the use of async client in the docs as well as a sort of info box.

@maldoinc maldoinc merged commit 10dbb11 into maldoinc:master Dec 21, 2025
8 checks passed
@maldoinc
Copy link
Owner

Looks good. Thanks for yet another contribution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants