Extension for Django Rest Framework to enable simple sideloading
Project description
Extention for Django Rest Framework to enable simple sidloading
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
Common Example of using library in ViewSet
class ProductViewSet(SideloadableRelationsMixin, viewsets.ModelViewSet):
"""
A simple ViewSet for viewing and editing products.
"""
queryset = Product.objects.all()
serializer_class = ProductSerializer
sideloadable_relations = {
'product': {'primary':True, 'serializer': ProductSerializer},
'category': {'serializer': CategorySerializer, 'name': 'categories'},
'supplier': SupplierSerializer,
'partner': PartnerSerializer
}
To test it out send GET request:
GET /product/?sideload=category,partner,supplier
Response looks like:
{
"category": [
{
"id": 1,
...
}
],
"partner": [
{
"id": 1,
...
},
{
"id": 2,
...
},
{
"id": 3,
...
}
],
"product": [
{
"id": 1,
"name": "Product 1",
"category": 1,
"supplier": 1,
"partner": [
1,
2,
3
]
}
],
"supplier": [
{
"id": 1,
...
}
]
}
Features
- sideloadable_relations dict values supports following types
serializers.Serializer or subclass
- dictionary with following keys
primary - indicates primary model
serializer - serializer class
name - override name of the sideloaded relation
note: invalid or unexisting relation names will be ignored and only valid relation name matches will be used
TODO
fix documentation
improve coverage
python3 support
Running Tests
Does the code actually work?
source <YOURVIRTUALENV>/bin/activate (myenv) $ pip install tox (myenv) $ tox
Credits
Tools used in rendering this package:
History
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.7-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ae5afdf10b985b7dbdcc0fc1aed7dbfd6df79cec75402bfb1f1f6811f0e32ae |
|
MD5 | c6cf843b8f0b1d59f982e933302fcf5f |
|
BLAKE2b-256 | 47d50c5fdacbeda9dc3b7d48d4d69fe4d6ed5161ee60029b74e980657c41ebf8 |