Extension for Django Rest Framework to enable simple sideloading
Project description
Extension for Django Rest Framework to enable simple sideloading
Documentation
The full documentation is at https://drf-sideloading.readthedocs.io.
Quickstart
Install drf-sideloading:
pip install drf-sideloading
Import Mixin SideloadableRelationsMixin:
from drf_sideloading.mixins import SideloadableRelationsMixin
Include mixin in view and define serializers dict sideloadable_relations as shown in examples
It is required to define and indicate primary relationship in sideloadable_relations dict
Example of using mixin in ViewSet
class ProductViewSet(SideloadableRelationsMixin, viewsets.ModelViewSet):
"""
A simple ViewSet for viewing and editing products.
"""
queryset = Product.objects.all()
serializer_class = ProductSerializer
sideloadable_relations = {
'products': {'primary': True, 'serializer': ProductSerializer},
'categories': {'serializer': CategorySerializer, 'source': 'category', 'prefetch': ['category']},
'suppliers': {'serializer': SupplierSerializer, 'source': 'supplier', 'prefetch': ['supplier']},
'partners': {'serializer': PartnerSerializer, 'source': 'partners', 'prefetch': ['partners']}
}
To test it out send GET request:
GET /product/?sideload=categories,partners,suppliers
Response looks like:
{
"categories": [
{
"id": 1,
...
}
],
"partners": [
{
"id": 1,
...
},
{
"id": 2,
...
},
{
"id": 3,
...
}
],
"products": [
{
"id": 1,
"name": "Product 1",
"category": 1,
"supplier": 1,
"partner": [
1,
2,
3
]
}
],
"suppliers": [
{
"id": 1,
...
}
]
}
Example Project
directory example includes example project you can setup and run int locally using following commands
cd example sh scripts/devsetup.sh sh scripts/dev.sh
Running Tests
Does the code actually work?
source <YOURVIRTUALENV>/bin/activate (myenv) $ pip install tox (myenv) $ tox
manually specify env
export TOX_ENV=py36-django18-drf34 tox -e $TOX_ENV
# TODO
fix documentation
improve coverage
Credits
Tools used in rendering this package:
History
0.1.8 (2017-07-20)
change sideloadable_relations dict
always required to define ‘serializer’
key is referenced to url and serialized in as rendered json
add source which specifies original model field name
0.1.0 (2017-07-20)
First release on PyPI.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for drf_sideloading-0.1.8-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f192788dbc3f574be1c0f1e01e0a67f064e2fcd7bd6d081a4ddaae80d0a1f3b7 |
|
MD5 | 239a7fe61abdbf595c2d8a0601fef95f |
|
BLAKE2b-256 | 98b57c4e57dc7af8749d18ae4e01665306326adc372caea7b95cd5d093d84fc0 |