{"id":20977318,"url":"https://github.com/2degrees/drf-nested-resources","last_synced_at":"2025-05-14T14:32:05.865Z","repository":{"id":32440255,"uuid":"36018463","full_name":"2degrees/drf-nested-resources","owner":"2degrees","description":"Support for nested routes in the Django REST Framework","archived":false,"fork":false,"pushed_at":"2020-02-27T07:07:29.000Z","size":99,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T19:11:21.707Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/2degrees.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-05-21T14:45:44.000Z","updated_at":"2024-01-15T13:04:37.000Z","dependencies_parsed_at":"2022-07-22T08:22:03.785Z","dependency_job_id":null,"html_url":"https://github.com/2degrees/drf-nested-resources","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2degrees%2Fdrf-nested-resources","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2degrees%2Fdrf-nested-resources/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2degrees%2Fdrf-nested-resources/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/2degrees%2Fdrf-nested-resources/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/2degrees","download_url":"https://codeload.github.com/2degrees/drf-nested-resources/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254160648,"owners_count":22024574,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T04:58:18.329Z","updated_at":"2025-05-14T14:32:05.542Z","avatar_url":"https://github.com/2degrees.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# drf-nested-resources\n\nThis is a django rest framework extension to allow developers to create nested\nresources.\n\n## How to use\n\n### Configuration of nested resources\n\nFor this example we are going to create a simple API with the following\nendpoints:\n\n    /developers/\n    /developers/\u003cid\u003e\n    /developers/\u003cid\u003e/languages/\n    /developers/\u003cid\u003e/languages/\u003cid\u003e\n\nFirst we start with the following Django models:\n\n```python\nfrom django.db.models import CASCADE\nfrom django.db.models.base import Model\nfrom django.db.models.fields import CharField\nfrom django.db.models.fields.related import ForeignKey\n\n\nclass Developer(Model):\n\n    name = CharField(max_length=20)\n\n\nclass ProgrammingLanguage(Model):\n\n    name = CharField(max_length=20)\n\n    author = ForeignKey(\n        Developer, \n        related_name='programming_languages', \n        on_delete=CASCADE,\n    )\n```\n\nWe will have the two viewsets for both the `developers` and `languages` resource\ncollections.\n\n```python\nfrom rest_framework.viewsets import ModelViewSet\nfrom drf_nested_resources.fields import HyperlinkedNestedModelSerializer\n\n\nclass _DeveloperSerializer(HyperlinkedNestedModelSerializer):\n\n    class Meta(object):\n\n        model = Developer\n\n        fields = ('url', 'name', 'programming_languages')\n\n\nclass DeveloperViewSet(ModelViewSet):\n\n    queryset = Developer.objects.all()\n\n    serializer_class = _DeveloperSerializer\n\n\nclass _ProgrammingLanguageSerializer(HyperlinkedNestedModelSerializer):\n\n    class Meta(object):\n\n        model = ProgrammingLanguage\n\n        fields = ('url', 'name', 'author')\n\n\nclass ProgrammingLanguageViewSet(ModelViewSet):\n\n    queryset = ProgrammingLanguage.objects.all()\n\n    serializer_class = _ProgrammingLanguageSerializer\n```\n\nThe related fields in the ViewSets `author` and `programming_languages` should\nfollow the model representation so that `author` will give us a url for the\ndeveloper who wrote the ProgrammingLanguage and the `programming_languages`\nshould give us a list of urls for the ProgrammingLanguages that the Developer\nwrote.\n\nThis is how you would generate the urlpatterns for them:\n\n```python\n_RESOURCES = [\n    Resource(\n        'developer',\n        'developers',\n        DeveloperViewSet,\n        [\n            NestedResource(\n                'language',\n                'languages',\n                ProgrammingLanguageViewSet,\n                parent_field_lookup='author',\n                )\n            ],\n        ),\n    ]\nurlpatterns = make_urlpatterns_from_resources(_RESOURCES)\n```\n\nFor more examples of different relationships and authorization check the test\nsuite.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F2degrees%2Fdrf-nested-resources","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F2degrees%2Fdrf-nested-resources","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F2degrees%2Fdrf-nested-resources/lists"}