Skip to main content

drf-nested-resources

This is a django rest framework extension to allow developers to create nested resources.

How to use

Configuration of nested resources

For this example we are going to create a simple API with the following endpoints:

/developers/
/developers/<id>
/developers/<id>/languages/
/developers/<id>/languages/<id>

First we start with the following Django models:

from django.db.models import CASCADE
from django.db.models.base import Model
from django.db.models.fields import CharField
from django.db.models.fields.related import ForeignKey


class Developer(Model):

    name = CharField(max_length=20)


class ProgrammingLanguage(Model):

    name = CharField(max_length=20)

    author = ForeignKey(
        Developer, 
        related_name='programming_languages', 
        on_delete=CASCADE,
    )

We will have the two viewsets for both the developers and languages resource collections.

from rest_framework.viewsets import ModelViewSet
from drf_nested_resources.fields import HyperlinkedNestedModelSerializer


class _DeveloperSerializer(HyperlinkedNestedModelSerializer):

    class Meta(object):

        model = Developer

        fields = ('url', 'name', 'programming_languages')


class DeveloperViewSet(ModelViewSet):

    queryset = Developer.objects.all()

    serializer_class = _DeveloperSerializer


class _ProgrammingLanguageSerializer(HyperlinkedNestedModelSerializer):

    class Meta(object):

        model = ProgrammingLanguage

        fields = ('url', 'name', 'author')


class ProgrammingLanguageViewSet(ModelViewSet):

    queryset = ProgrammingLanguage.objects.all()

    serializer_class = _ProgrammingLanguageSerializer

The related fields in the ViewSets author and programming_languages should follow the model representation so that author will give us a url for the developer who wrote the ProgrammingLanguage and the programming_languages should give us a list of urls for the ProgrammingLanguages that the Developer wrote.

This is how you would generate the urlpatterns for them:

_RESOURCES = [
    Resource(
        'developer',
        'developers',
        DeveloperViewSet,
        [
            NestedResource(
                'language',
                'languages',
                ProgrammingLanguageViewSet,
                parent_field_lookup='author',
                )
            ],
        ),
    ]
urlpatterns = make_urlpatterns_from_resources(_RESOURCES)

For more examples of different relationships and authorization check the test suite.

Changelog

Version 2.0.0

Added support for Django 2.2 and removed support for Django < 2.2

Version 1.3

Updated dependencies and added coverage config

Version 1.2 Beta 3

Fixed error when urlconf was not explicitly set on the request

Version 1.2 Beta 2

Fixed support for non-session-based authenticators when checking permissions in viewsets in the ancestry.

Version 1.2 Beta 1

Implemented ability to force a field value in a nested resource to be set to the URL of an ascending resource. This is done using the optional attribute field_forced_to_ancestor on the serializer's Meta class.

Version 1.1

Re-worked the mechanism for URL generation to support cross-linking resource trees.

Breaking change: Any previous usage of many-to-many fields on variables in the current request's URL will now break.

Version 1.0 Release Candidate 3

Added proper support for namespaced URLs

Version 1.0 Release Candidate 1

Added support for Django 1.10 and Rest Framework 3.4.3

Version 1.0 Beta 1 (unreleased)

Initial release.

Release files for drf-nested-resources 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for drf-nested-resources 2.0.0
File Size Uploaded
drf-nested-resources-2.0.0.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for drf-nested-resources 2.0.0
File Interpreter ABI Platform
drf_nested_resources-2.0.0-py2-none-any.whl Python 2 none any Details

Total release size: 21.3 kB

Release files / drf-nested-resources-2.0.0.tar.gz

Download URL drf-nested-resources-2.0.0.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7aa10cb1fb190b62265bca72da2b2a68ab0bf5c3f091dc87786193fe845633c1
BLAKE2b-256 checksum
How to use checksums
f84603ccf4f1fd3d1e894848566ff595faeb511a15b08585b690c503496be078
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

Release files / drf_nested_resources-2.0.0-py2-none-any.whl

Download URL drf_nested_resources-2.0.0-py2-none-any.whl
Size 12.4 kB
Tags Python 2
SHA-256 checksum
How to use checksums
cd8ae4110d559ca70e9e2217ff5a405bb4db7c2ad8a0c8cacc882df3b8c57778
BLAKE2b-256 checksum
How to use checksums
47ed4292b40331460f24f80b8b4208d12fa87250b6968cde4fe27cb0bc1f3681
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.1 setuptools/41.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page