| 1 |
--- |
| 2 |
title: Asynchronous views |
| 3 |
description: Using PermissionRequiredMixin with asynchronous class-based views. |
| 4 |
--- |
| 5 |
|
| 6 |
# Asynchronous views |
| 7 |
|
| 8 |
`guardian.mixins.PermissionRequiredMixin` works with asynchronous |
| 9 |
class-based views. Django detects a view as asynchronous when all of its |
| 10 |
HTTP handlers (`get()`, `post()`, ...) are coroutines. When that is the |
| 11 |
case, the mixin runs its permission check asynchronously instead of the |
| 12 |
synchronous one. |
| 13 |
|
| 14 |
!!! note |
| 15 |
Asynchronous class-based views require Django >= 4.2. |
| 16 |
|
| 17 |
Synchronous views are unaffected: they keep using `check_permissions()` |
| 18 |
and `get_permission_object()` exactly as before, and the only addition on |
| 19 |
their path is a single flag check in `dispatch()`. |
| 20 |
|
| 21 |
## Basic usage |
| 22 |
|
| 23 |
Nothing special is required. Declare the handlers as coroutines and the |
| 24 |
mixin does the rest: |
| 25 |
|
| 26 |
```python |
| 27 |
from django.http import HttpResponse |
| 28 |
from django.views.generic import View |
| 29 |
|
| 30 |
from guardian.mixins import PermissionRequiredMixin |
| 31 |
|
| 32 |
|
| 33 |
class PostView(PermissionRequiredMixin, View): |
| 34 |
permission_required = 'testapp.change_post' |
| 35 |
raise_exception = True |
| 36 |
|
| 37 |
async def get(self, request, *args, **kwargs): |
| 38 |
return HttpResponse('some html') |
| 39 |
``` |
| 40 |
|
| 41 |
By default, the object to check the permission against is resolved by |
| 42 |
`aget_permission_object()`, which runs the synchronous |
| 43 |
`get_permission_object()` in a thread. That is, `permission_object`, |
| 44 |
`get_object()` and `object` are honoured as usual. |
| 45 |
|
| 46 |
## Fetching the object with the asynchronous ORM |
| 47 |
|
| 48 |
To avoid the thread hop and use the asynchronous ORM API, override |
| 49 |
`aget_permission_object()`: |
| 50 |
|
| 51 |
```python |
| 52 |
class PostView(PermissionRequiredMixin, View): |
| 53 |
permission_required = 'testapp.change_post' |
| 54 |
raise_exception = True |
| 55 |
|
| 56 |
async def aget_permission_object(self): |
| 57 |
return await Post.objects.aget(slug=self.kwargs['slug']) |
| 58 |
|
| 59 |
async def get(self, request, *args, **kwargs): |
| 60 |
return HttpResponse('some html') |
| 61 |
``` |
| 62 |
|
| 63 |
An `async def get_object()` or an `async def get_permission_object()` on the |
| 64 |
view is awaited as well, so this works too: |
| 65 |
|
| 66 |
```python |
| 67 |
class PostView(PermissionRequiredMixin, View): |
| 68 |
permission_required = 'testapp.change_post' |
| 69 |
|
| 70 |
async def get_object(self): |
| 71 |
return await Post.objects.aget(slug=self.kwargs['slug']) |
| 72 |
|
| 73 |
async def get(self, request, *args, **kwargs): |
| 74 |
return HttpResponse('some html') |
| 75 |
``` |
| 76 |
|
| 77 |
## Asynchronous counterparts |
| 78 |
|
| 79 |
Each synchronous method has an `a`-prefixed sister method, following the |
| 80 |
naming convention used by Django itself (`aget()`, `acreate()`, ...): |
| 81 |
|
| 82 |
| Synchronous | Asynchronous | |
| 83 |
|---------------------------|----------------------------| |
| 84 |
| `get_permission_object()` | `aget_permission_object()` | |
| 85 |
| `check_permissions()` | `acheck_permissions()` | |
| 86 |
| `dispatch()` | `adispatch()` | |
| 87 |
|
| 88 |
`get_required_permissions()`, `get_object_permission_denied_message()` and |
| 89 |
`on_permission_check_fail()` have no asynchronous counterpart: they are all |
| 90 |
called from a single worker thread, so they may run queries. |
| 91 |
|
| 92 |
If you override `check_permissions()`, override `acheck_permissions()` as |
| 93 |
well; asynchronous views do not call the synchronous one. |
| 94 |
|
| 95 |
## Limitations |
| 96 |
|
| 97 |
- Django's generic views (`DetailView`, `ListView`, ...) provide synchronous |
| 98 |
handlers by default. They use the mixin's synchronous path unless every |
| 99 |
effective HTTP handler is overridden with a coroutine. |
| 100 |
- `LoginRequiredMixin` and `PermissionListMixin` have no asynchronous |
| 101 |
support yet. |
| 102 |
- On Django 4.1 and older the synchronous path is used even for |
| 103 |
asynchronous views. Responses for a disallowed HTTP method only became |
| 104 |
awaitable in Django 4.1.2, and 4.1 is end of life, so the floor is 4.2. |