Skip to content

Extension for Django REST Framework 3 which allows for using nested resources

License

Notifications You must be signed in to change notification settings

jerynmathew/drf-nested-routing

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

drf-nested-routing

Extension for Django REST Framework 3 which allows for usage nested resources.

Setup

pip install drf-nested-routing

Requirement

  • Python 2.7+
  • Django 1.6+
  • Django REST Framework 3

Example

models.py:

class TestResource(models.Model):
	name = models.CharField(max_length=255)
	active = models.BooleanField(default=True)

class NestedResource(models.Model):
	resource = models.ForeignKey(TestResource)
	name = models.CharField(max_length=255)

serializers.py:

class TestResourceSerializer(HyperlinkedModelSerializer):
	class Meta:
    	model = TestResource

class NestedResourceSerializer(NestedRoutingSerializerMixin, HyperlinkedModelSerializer):
	class Meta:
    	model = NestedResource

views.py:

class TestResourceViewSet(ModelViewSet):
	serializer_class = TestResourceSerializer
	queryset = TestResource.objects.all()

class NestedResourceViewSet(NestedViewSetMixin, ModelViewSet):
	serializer_class = NestedResourceSerializer
	queryset = NestedResource.objects.all()

urls.py

class NestedSimpleRouter(NestedRouterMixin, SimpleRouter):
	pass

router = NestedSimpleRouter()
resourceRoute = router.register(r'test-resources', TestResourceViewSet)
resourceRoute.register(r'nested', NestedResourceViewSet, ['resource'])

urlpatterns = patterns(	
	'',
	url(r'', include(router.urls)),
)

About

Extension for Django REST Framework 3 which allows for using nested resources

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%