Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/2degrees/drf-nested-resources
Support for nested routes in the Django REST Framework
https://github.com/2degrees/drf-nested-resources
Last synced: about 14 hours ago
JSON representation
Support for nested routes in the Django REST Framework
- Host: GitHub
- URL: https://github.com/2degrees/drf-nested-resources
- Owner: 2degrees
- License: bsd-3-clause
- Created: 2015-05-21T14:45:44.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-02-27T07:07:29.000Z (over 4 years ago)
- Last Synced: 2024-04-16T01:25:19.349Z (7 months ago)
- Language: Python
- Size: 96.7 KB
- Stars: 7
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# 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/
/developers//languages/
/developers//languages/First we start with the following Django models:
```python
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 ForeignKeyclass 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.```python
from rest_framework.viewsets import ModelViewSet
from drf_nested_resources.fields import HyperlinkedNestedModelSerializerclass _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:
```python
_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.