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, define serializers dict sideloadable_relations and base_model_name
Defining primary relationship is optional if defined defaults will be overrided In below case we define primary relationship along with side ones. By adding ‘product’: {‘primary’:True}, 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},
'category': CategorySerializer,
'supplier': SupplierSerializer,
'partner': PartnerSerializer
}
Add extra parameter and define comma separated relations:
GET /product/?sideload=category,partner,supplier
{
"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
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.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b5863eb39a62db6cc4a606af7854cd55add860f49a77c82dca8bee5d0b0a333 |
|
MD5 | 7dcfb02b3367003710cebd0567c9a0e2 |
|
BLAKE2b-256 | 24cca07cfc5db0355cc2f820005276c35857e352dc65bd1e6238537d8395d5b0 |