drf-sideloading¶
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 a 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'}
}
Request:
GET /product/?sideload=categories,partners,suppliers
Response:
{
"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
Contributing¶
For detailed description see CONTRIBUTING Notes
Setup for contribution
source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements_dev.txt
Test with specific env
$ tox --listenvs
py27-django18-drf34
py27-django19-drf34
# ...
$ tox -e py27-django19-drf34
Test coverage
$ make coverage
Use pyenv for testing using different versions locally
# TODO
- fix documentation
- improve coverage