Created
December 17, 2012 16:22
-
-
Save eofs/4319576 to your computer and use it in GitHub Desktop.
Test project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from models import ClassA | |
class ClassASerializer(serializers.ModelSerializer): | |
childs = serializers.ManyPrimaryKeyRelatedField() | |
class Meta: | |
model = ClassA |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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