---
title: "adrf | pypx"
description: "Async support for Django REST framework"
canonical_url: "https://pypx.app/packages/adrf"
meta:
  "og:description": "Async support for Django REST framework"
  "og:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_adrf%2Cversion_0.1.14%2Csummary_Async%2Bsupport%2Bfor%2BDjango%2BREST%2Bframework%2Clicense_MIT%2BLicense%2Cp_Ii9wYWNrYWdlcy9hZHJmIg%2Cs_Nyw7gBz9dT8Xv1NU.png"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "adrf 0.1.14"
  "twitter:image": "https://pypx.app/_og/d/h_630%2Cc_PackageCard%2Cname_adrf%2Cversion_0.1.14%2Csummary_Async%2Bsupport%2Bfor%2BDjango%2BREST%2Bframework%2Clicense_MIT%2BLicense%2Cp_Ii9wYWNrYWdlcy9hZHJmIg%2Cs_Nyw7gBz9dT8Xv1NU.png"
---

> Index of every page on this site: [llms.txt](https://pypx.app/llms.txt)

# **adrf**

v0.1.14

Async support for Django REST framework

22 KB Python 3.8+ py3-none-anyMIT License3 deps

[** Diff **](https://pypx.app/packages/adrf/diff)

`$ uv add adrf`

# Async Django REST framework

**Async support for Django REST framework**

# Requirements

- Python 3.8+
- Django 4.1+

We **highly recommend** and only officially support the latest patch release of each Python and Django series.

# Installation

Install using `pip`...

```
pip install adrf
```

Add `'adrf'` to your `INSTALLED_APPS` setting.

```
INSTALLED_APPS = [
    ...
    'adrf',
]
```

# Examples

# Async Views

When using Django 4.1 and above, this package allows you to work with async class and function based views.

For class based views, all handler methods must be async, otherwise Django will raise an exception. For function based views, the function itself must be async.

For example:

```
from adrf.views import APIView

class AsyncAuthentication(BaseAuthentication):
    async def authenticate(self, request) -> tuple[User, None]:
        return user, None

class AsyncPermission:
    async def has_permission(self, request, view) -> bool:
        if random.random() < 0.7:
            return False

        return True

    async def has_object_permission(self, request, view, obj):
        if obj.user == request.user or request.user.is_superuser:
            return True

        return False

class AsyncThrottle(BaseThrottle):
    async def allow_request(self, request, view) -> bool:
        if random.random() < 0.7:
            return False

        return True

    def wait(self):
        return 3

class AsyncView(APIView):
    authentication_classes = [AsyncAuthentication]
    permission_classes = [AsyncPermission]
    throttle_classes = [AsyncThrottle]

    async def get(self, request):
        return Response({"message": "This is an async class based view."})

from adrf.decorators import api_view

@api_view(['GET'])
async def async_view(request):
    return Response({"message": "This is an async function based view."})
```

# Async ViewSets

For viewsets, all handler methods must be async too.

views.py

```
from django.contrib.auth import get_user_model
from rest_framework.response import Response

from adrf.viewsets import ViewSet


User = get_user_model()


class AsyncViewSet(ViewSet):

    async def list(self, request):
        return Response(
            {"message": "This is the async `list` method of the viewset."}
        )

    async def retrieve(self, request, pk):
        user = await User.objects.filter(pk=pk).afirst()
        return Response({"user_pk": user and user.pk})

```

# ModelViewSet Routing

The `ModelViewSet` implementation included with adrf provides asynchronous CRUD actions (e.g., `aretrieve`, `acreate`, `aupdate`, and `alist`). However, these actions are not invoked by default when using the router implementation in `rest_framework`. To have these async methods mapped by default, use adrf's router instead:

urls.py

```
from django.urls import path, include
from adrf import routers    # import the adrf router instead

from . import views

router = routers.DefaultRouter()
router.register(r"async_viewset", views.AsyncModelViewSet, basename="async")

urlpatterns = [
    path("", include(router.urls)),
]

```

# Async Serializers

serializers.py

```
from adrf.serializers import Serializer
from rest_framework import serializers

class AsyncSerializer(Serializer):
    username = serializers.CharField()
    password = serializers.CharField()
    age = serializers.IntegerField()
```

views.py

```
from .serializers import AsyncSerializer
from adrf.views import APIView

class AsyncView(APIView):
    async def get(self, request):
        data = {
            "username": "test",
            "password": "test",
            "age": 10,
        }
        serializer = AsyncSerializer(data=data)
        serializer.is_valid()
        return await serializer.adata
```

# Async Generics

models.py

```
from django.db import models

class Order(models.Model):
    name = models.TextField()
```

serializers.py

```
from adrf.serializers import ModelSerializer
from .models import Order

class OrderSerializer(ModelSerializer):
    class Meta:
        model = Order
        fields = ('name', )
```

views.py

```
from adrf.generics import ListCreateAPIView
from .models import Order
from .serializers import OrderSerializer

class ListCreateOrderView(ListCreateAPIView):
    queryset = Order.objects.all()
    serializer_class = OrderSerializer
```

## **Details **

<dl>

<dt>Version</dt>
<dd>0.1.14</dd>

<dt>License</dt>
<dd>MIT License</dd>

<dt>Python</dt>
<dd>&gt;=3.8</dd>

<dt>Maintainer</dt>
<dd>Enrico Massa</dd></dl>

## **Links**

- [Homepage](https://github.com/em1208/adrf)
- [Repository](https://github.com/em1208/adrf)
- [View on PyPI](https://pypi.org/project/adrf/)

## **Release Cadence **

**3**

releases in the past year

avg 97 days between releases

## **Maintainers **

- Enrico Massa · [enrico.massa@gmail.com](mailto:enrico.massa@gmail.com)

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://pypx.app/#website","@type":"WebSite","description":"The Python Package Index, reimagined. Fast search, dependency insights, download trends, and changelogs — all in one place.","name":"pypx","url":"https://pypx.app/"},{"@id":"https://pypx.app/packages/adrf#webpage","@type":"WebPage","description":"Async support for Django REST framework","name":"adrf","url":"https://pypx.app/packages/adrf","isPartOf":{"@id":"https://pypx.app/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://pypx.app/packages/adrf"]}]},{"@id":"https://pypx.app/packages/adrf#/schema/software-application/1","@type":"SoftwareApplication","applicationCategory":"DeveloperApplication","description":"Async support for Django REST framework","license":"MIT License","name":"adrf","softwareVersion":"0.1.14"}]}
```

## Sitemap

See the full [sitemap](https://pypx.app/sitemap.md) for all pages.
