#37065: @method_decorator can't be used on async view at class-level with
name="dispatch"
-----------------------------+--------------------------------------
Reporter: Jacob Walls | Owner: Vishy Algo
Type: Bug | Status: assigned
Component: Utilities | Version: 5.2
Severity: Normal | Resolution:
Keywords: async | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------------------------
Comment (by Carlton Gibson):
This is a doozy. I'm not convinced it's tractable... (at reasonable
cost/complexity to method_decorator).
If the method being decorated is not async then it's not 100% clear to me
that method_decorator should do **any** further introspection to determine
if the return value should be marked as a coroutine.
We're asking it to decorate a method, and #35083 was about allowing those
methods to be async. Both conjuncts work.
> Moving the decorator to the method instead of the class works.
A further ''know significant details about the particulars of View''
strikes me as out of scope for method_decorator.
If I **had to** decorate `dispatch` for an async view class, I think this
is probably the best option:
{{{
@method_decorator(never_cache, name="dispatch")
class MyClass(View):
async def dispatch(self, *args, **kwargs):
return super().dispatch(*args, **kwargs)
async def get(self, request):
return HttpResponse()
}}}
(Not tested. You get the idea)
This somewhat mitigates using the `name="dispatch"` — so I'd probably put
it on the method directly, and I'd only need to do ''that'' if we also had
say `async def post` as well.
But I think trying to automagically our way forward here is mistake. It's
complex. It just is.
So I think a docs tweak is probably the way to go.
--
Ticket URL: <https://code.djangoproject.com/ticket/37065#comment:7>
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 visit
https://groups.google.com/d/msgid/django-updates/0107019e2c80a9d0-07e16fe2-8229-471e-b528-06ff39a9bb81-000000%40eu-central-1.amazonses.com.