Welcome to drf-sideloading’s documentation!

Contents:

drf-sideloading

Package Index Build Status Code Coverage Documentation Status License is MIT

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

Credits

Tools used in rendering this package:

Installation

At the command line:

$ easy_install drf-sideloading

Or, if you have virtualenvwrapper installed:

$ mkvirtualenv drf-sideloading
$ pip install drf-sideloading

Usage

Import siedloading mixin

from drf_sideloading.mixins import SideloadableRelationsMixin

Use in existing viewset by inheriting and defining sideloadable_relations


class ProductViewSet(SideloadableRelationsMixin, viewsets.ModelViewSet):
    """
    A simple ViewSet for viewing and editing products.
    """
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    sideloading_serializer_class = ProductSideloadableSerializer


class CategoryViewSet(SideloadableRelationsMixin, viewsets.ModelViewSet):
    """
    A more complex ViewSet with reverse relations.

Request:

GET /product/?sideload=categories,partners,suppliers

drf_sideloading

drf_sideloading package

Submodules

drf_sideloading.mixins module

class drf_sideloading.mixins.SideloadableRelationsMixin(**kwargs)[source]

Bases: object

TODO: Implement some protection for too large queries.
  • limit the number of sideloadable elements?
    if over limit: - raise error - show warning - paginate, show first page and add a link to remaining paginated list of related elements? - show only the link to paginated list of related elements?
check_sideloading_serializer_class()[source]
get_primary_field_name()[source]
get_relevant_prefetches()[source]
get_sideloadable_fields()[source]
get_sideloadable_page(page)[source]
get_sideloadable_page_from_queryset(queryset)[source]
get_sideloading_prefetches()[source]
list(request, *args, **kwargs)[source]
parse_query_param(sideload_parameter)[source]

Parse query param and take validated names

:param sideload_parameter string :return valid relation names list

comma separated relation names may contain invalid or unusable characters. This function finds string match between requested names and defined relation in view

query_param_name = u'sideload'
relations_to_sideload = None
sideloading_serializer_class = None

drf_sideloading.serializers module

class drf_sideloading.serializers.SideLoadableSerializer(instance=None, data=<class rest_framework.fields.empty>, **kwargs)[source]

Bases: rest_framework.serializers.Serializer

to_representation(instance)[source]

Object instance -> Dict of primitive datatypes.

Module contents

Contributing

Contributions are welcome, and they are greatly appreciated! Every little bit helps, and credit will always be given.

You can contribute in many ways:

Types of Contributions

Report Bugs

Report bugs at https://github.com/namespace-ee/drf-sideloading/issues.

If you are reporting a bug, please include:

  • Your operating system name and version.
  • Any details about your local setup that might be helpful in troubleshooting.
  • Detailed steps to reproduce the bug.

Fix Bugs

Look through the GitHub issues for bugs. Anything tagged with “bug” is open to whoever wants to implement it.

Implement Features

Look through the GitHub issues for features. Anything tagged with “feature” is open to whoever wants to implement it.

Write Documentation

drf-sideloading could always use more documentation, whether as part of the official drf-sideloading docs, in docstrings, or even on the web in blog posts, articles, and such.

Submit Feedback

The best way to send feedback is to file an issue at https://github.com/namespace-ee/drf-sideloading/issues.

If you are proposing a feature:

  • Explain in detail how it would work.
  • Keep the scope as narrow as possible, to make it easier to implement.
  • Remember that this is a volunteer-driven project, and that contributions are welcome :)

Get Started!

Ready to contribute? Here’s how to set up drf-sideloading for local development.

  1. Fork the drf-sideloading repo on GitHub.

  2. Clone your fork locally:

    $ git clone git@github.com:namespace-ee/drf-sideloading.git
    
  3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:

    $ mkvirtualenv drf-sideloading
    $ cd drf-sideloading/
    $ python setup.py develop
    
  4. Create a branch for local development:

    $ git checkout -b name-of-your-bugfix-or-feature
    

    Now you can make your changes locally.

  5. When you’re done making changes, check that your changes pass flake8 and the tests, including testing other Python versions with tox:

    $ flake8 drf_sideloading tests
    $ python manage.py test
    $ tox
    

    To get flake8 and tox, just pip install them into your virtualenv.

  6. Commit your changes and push your branch to GitHub:

    $ git add .
    $ git commit -m "Your detailed description of your changes."
    $ git push origin name-of-your-bugfix-or-feature
    
  7. Submit a pull request through the GitHub website.

Pull Request Guidelines

Before you submit a pull request, check that it meets these guidelines:

  1. The pull request should include tests.
  2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst.
  3. The pull request should work for Python 2.6, 2.7, and 3.3, and for PyPy. Check https://travis-ci.org/namespace-ee/django-rest-framework-sideloading/pull_requests and make sure that the tests pass for all supported Python versions.

Tips

To run a subset of tests:

$ python -m unittest tests.test_drf_sideloading

Credits

Contributors

None yet. Why not be the first?

History

0.1.10 (2017-07-20)

  • Support for django2.0

0.1.8 (2017-07-20)

  • change sideloadable_relations dict
  • always required to define ‘serializer’
  • key is referenced to url and serialized in as rendered json
  • add source which specifies original model field name

0.1.0 (2017-07-20)

  • First release on PyPI.