Skip to main content

Extension for Django Rest Framework to enable simple sideloading

Project description

Package Index Build Status Code Coverage Documentation Status PyPI Downloads License is MIT

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf-sideloading-0.1.6.tar.gz (7.5 kB view hashes)

Uploaded Source

Built Distribution

drf_sideloading-0.1.6-py2.py3-none-any.whl (7.5 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page