Support for nested routes in the Django REST Framework
Project description
# 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:
```python
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')
```
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 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:
```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.
Changelog
=========
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.
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:
```python
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')
```
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 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:
```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.
Changelog
=========
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for drf-nested-resources-1.0rc3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4045af5e1942553adf2bd23b563040c92ea9043e6e67a7a3a3c6ec472c399dce |
|
MD5 | 339a142546d263d5d312f942d82158c4 |
|
BLAKE2b-256 | af59345e315d63b8460c1612f06d776a96ce5af855db54359e19bfcf9096a072 |
Close
Hashes for drf_nested_resources-1.0rc3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 729e8bf531e36a51ea7fd9c82d2e1b2e39f2ac7863d3174ec6c53871b27e565a |
|
MD5 | 48a1b6546108e92f1a771fa939d4d122 |
|
BLAKE2b-256 | d13d557770465f8de30763de8ca88d9f85c5728710f8e369729bfd3189045706 |