rest_framework_channels
The enhanced modules for REST WebSockets using django channels.
Installation
pip install rest_framework_channels
After installing it, you should insert 'rest_framework_channels' in the INSTALLED_APPS.
INSTALLED_APPS = [
# Websocket
'daphne',
'channels',
'rest_framework_channels', # add
...
Introduction
rest_framework_channels is the enhanced modules for REST WebSockets using django channels.
You can use serializers and queryset in rest_framework in rest_framework_channels. Also, we are ready for similar permissions and generics too.
Example
We use the below model and serializer as example.
class TestModel(models.Model):
"""Simple model to test with."""
title = models.CharField(max_length=255)
content = models.CharField(max_length=1024)
class TestSerializer(ModelSerializer):
class Meta:
model = TestModel
fields = '__all__'
from rest_framework_channels import generics
from rest_framework_channels.consumers import AsyncAPIConsumer
from rest_framework_channels.permissions import IsAuthenticated
class ChildActionHandler(generics.RetrieveAPIActionHandler):
serializer_class = TestSerializer
queryset = TestModel.objects.all()
permission_classes = (IsAuthenticated,)
class ParentConsumer(AsyncAPIConsumer):
# You can define the routing inside the consumer similar with original django's urlpatterns
routepatterns = [
re_path(
r'test_child_route/(?P<pk>[-\w]+)/$',
ChildActionHandler.as_aaah(),
),
]
When you send the below json after establishing the connection,
{
'action': 'retrieve', # Similar with GET method of HTTP request
'route': 'test_child_route/1/',
}
you will get the below response. This mechanism is very similar with original rest_framework!
{
'errors': [],
'data': {
'id': 1,
'title': 'title',
'content': 'content'
},
'action': 'retrieve',
'route': 'test_child_route/1/',
'status': 200,
}
As you can see permission_classes, you will be rejected when you send that json without login.
{
'errors': ['Some Error Messages']
'data': None,
'action': 'retrieve',
'route': 'test_child_route/1/',
'status': 403,
}
Details
For more details, see docs.
Development
code
pip install -e .
pip install twine
documentation
cd sphinx
sudo apt-get -y install plantuml
pip install -r requirements.txt
- generate rst files and html files
cd sphinx
bash build.sh
Reference
This project is VERY inspired by djangochannelsrestframework.
Metadata
Release files for rest-framework-channels 0.0.7
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| rest_framework_channels-0.0.7.tar.gz | 20.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| rest_framework_channels-0.0.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 44.8 kB
Release files / rest_framework_channels-0.0.7.tar.gz
| Download URL | rest_framework_channels-0.0.7.tar.gz |
|---|---|
| Size | 20.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a587ed282b22237b9db35b8cbb5762d5bc7c48cfd2564b182a093c6e85c835bd
|
|
BLAKE2b-256 checksum How to use checksums |
175fad6f95c86295760103c907cbbb5f5619ce9dfe92c1d41000cb6fabd1d2a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / rest_framework_channels-0.0.7-py3-none-any.whl
| Download URL | rest_framework_channels-0.0.7-py3-none-any.whl |
|---|---|
| Size | 24.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a967c45f93db7c15abfb6aae093ce2213e6e6aa6450af05d7d5a7978e114d0cc
|
|
BLAKE2b-256 checksum How to use checksums |
2268d58123e451c221c0e57fde5dfaafc12071c908d3765d4424c623c53fdc29
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|