I have a basic setup using the Django Rest Framework. I have two models and a nested serializer setup:
# models.py
from django.db import models
class Plan(models.Model):
name = models.CharField(max_length='100')
def __unicode__(self):
return u'%s' % (self.name)
class Group(models.Model):
plan = models.ForeignKey('plan')
name = models.CharField(max_length='50')
weight = models.SmallIntegerField()
def __unicode__(self):
return u'%s - %s' % (self.name, self.plan.name)
# serializer.py
from plans.models import Plan, Group
from rest_framework import serializers
class GroupSerializer(serializers.ModelSerializer):
class Meta:
model = Group
fields = ('name', 'weight')
class PlanSerializer(serializers.ModelSerializer):
group = GroupSerializer(many=True, read_only=True)
class Meta:
model = Plan
fields = ('name', 'group')
# views.py
from rest_framework import viewsets
from plans.models import Plan
from plans.serializers import PlanSerializer
class PlanViewSet(viewsets.ModelViewSet):
queryset = Plan.objects.all()
serializer_class = PlanSerializer
When I view the serializers relationships in Django's Shell it shows the relationship correctly:
PlanSerializer():
name = CharField(max_length='100')
group = GroupSerializer(many=True, read_only=True):
name = CharField(max_length='50')
weight = IntegerField()
What I end up getting back via cURL is:
[
{
name: Test Plan
}
]
What I expect to get back is:
[
{
name: Test Plan,
group: [
{
name: Test Group,
weight: 1
}
]
}
]
There is no nested data coming through. I'm at a lose for what I've not setup correctly here. Can anyone point me in the correct direction?