Skip to content

Instantly share code, notes, and snippets.

@eofs
Created December 17, 2012 16:22
Show Gist options
  • Save eofs/4319576 to your computer and use it in GitHub Desktop.
Save eofs/4319576 to your computer and use it in GitHub Desktop.
Test project
from django.db import models
class ClassB(models.Model):
name = models.CharField(max_length=255)
class ClassA(models.Model):
name = models.CharField(max_length=255)
childs = models.ManyToManyField(ClassB, blank=True, null=True)
from rest_framework import serializers
from models import ClassA
class ClassASerializer(serializers.ModelSerializer):
childs = serializers.ManyPrimaryKeyRelatedField()
class Meta:
model = ClassA
from django.conf.urls import patterns, include, url
from demoapp.views import ObjectList
urlpatterns = patterns('demoapp.views',
url(r'^$', 'api_root'),
url(r'^objects/$', ObjectList.as_view(), name='object-list'),
)
from rest_framework import generics
from rest_framework.decorators import api_view
from rest_framework.reverse import reverse
from rest_framework.response import Response
from models import ClassA
from serializers import ClassASerializer
@api_view(['GET'])
def api_root(request, format=None):
return Response({
'objects': reverse('object-list', request=request)
})
class ObjectList(generics.ListCreateAPIView):
"""
List of objects
"""
model = ClassA
serializer_class = ClassASerializer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment