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
Defining primary relationship and indicating that it primary is required.
In below example we define primary relationship along with side ones. By adding ‘product’: {‘primary’:True, ‘serializer’: ProductSerializer}, in sideloadable_relations dict we
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': CategorySerializer,
'supplier': SupplierSerializer,
'partner': PartnerSerializer
}
To sideloaded relations add extra parameter and define comma separated relations in any order
GET /product/?sideload=category,partner,supplier
note: if invalid or unexisting relations are used it will be ignored and only valid relations will be loaded
{
"partner": [
{
"id": 1,
...
},
{
"id": 2,
...
},
{
"id": 3,
...
}
],
"categories": [
{
"id": 1,
...
}
],
"suppliers": [
{
"id": 1,
...
}
],
"products": [
{
"id": 1,
"name": "Product 1",
"category": 1,
"supplier": 1,
"partner": [
1,
2,
3
]
}
]
}
Another use case where you can change name of the loaded relation key
sideloadable_relations = {
'product': {'primary': True, 'serializer': ProductSerializer, 'name': 'products'},
'category': {'serializer': CategorySerializer, 'name': 'categories'},
'supplier': SupplierSerializer,
'partner': PartnerSerializer
}
Features
- sideloadable_relations dict values supports following types
serializers.Serializer or subclass
- dictionary with following keys
primary - to indicate primary model
serializer - serializer class
name - to override name of the sideloaded relation
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.6-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6098aaada03fae356f0ffe779dd8a7ebc2f95568e99a20349b5bb133b269d6e6 |
|
MD5 | 4dd9152867b98ad0e5dca14e4c0ef592 |
|
BLAKE2b-256 | 6b4ff8ddab7ece44d880cd9ed501a04c3789d6a44cbdc2f402d877d2a283cd1a |