title: Django-rest-framework, nested objects in Serializers
body: <p>I would like to have a nested object inside a serializer instead of just the foreignkey (or url).
  As <a href="http://www.django-rest-framework.org/api-guide/relations#nested-relationships" rel="nofollow noreferrer">this documentation</a> says, I just had to specify the serializer class of the nested object in the parent serializer:</p>
  
  <pre class="lang-python prettyprint-override"><code># Models
  
  class NestedSample(models.Model):
      something = models.CharField(max_length=255)
  
  class Sample(models.Model):
      thing = models.BooleanField()
      nested = models.ForeignKey(NestedSample)
  
  # Serializers
  
  class NestedSampleSerializer(serializers.HyperlinkedModelSerializer):
  
      class Meta:
          model = api_models.NestedSample
  
  class SampleSerializer(serializers.HyperlinkedModelSerializer):
      nested = NestedSampleSerializer() # HERE!
  
      class Meta:
          model = api_models.Sample
  
  # Views
  
  class NestedSampleViewSet(viewsets.ModelViewSet):
      queryset = api_models.NestedSample.objects.all()
      serializer_class = api_serializers.NestedSampleSerializer
  
  class SampleViewSet(viewsets.ModelViewSet):
      queryset = api_models.Sample.objects.all()
      serializer_class = api_serializers.SampleSerializer
  </code></pre>
  
  <p>This works very well when I get the objects, but it is not possible to create (=<code>POST</code>) <code>Sample</code> objects anymore, I get the error:</p>
  
  <pre><code>{u'non_field_errors': [u'Invalid data']}
  </code></pre>
  
  <p>I tried to overwrite the <code>create</code> method in the viewset to get the object using the pk:</p>
  
  <pre class="lang-python prettyprint-override"><code>class SampleViewSet(viewsets.ModelViewSet):
      queryset = api_models.Sample.objects.all()
      serializer_class = api_serializers.SampleSerializer
  
      def create(self, request):
          request.DATA['nested'] = get_object_or_404(api_models.NestedSample, pk=request.DATA['nested'])
          return super(SampleViewSet, self).create(request)
  </code></pre>
  
  <p>But it doesn't work as well.</p>
  
  <p>Any idea?</p>
  
  <p>I also found <a href="https://stackoverflow.com/questions/15883678/django-rest-framework-different-depth-for-post-put">this question</a> I can relate with which of course solves the problem but do not let me expose the full nested object, so back to the beginning.</p>
  
  <p>Thanks,</p>