title: django rest framework writable nested serializer returns empty nested data
body: <p>I'm following this link (<a href="http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers" rel="noreferrer">http://www.django-rest-framework.org/api-guide/relations/#writable-nested-serializers</a>) to write nested serializer.  But when I pop the 'vars' from validated_data in the create method of HostSerializer, I found it's empty.</p>
  
  <p>I'm using django 1.9.2 and django restframework 3.3.2.</p>
  
  <p><strong>My model:</strong></p>
  
  <pre><code>class Host(models.Model):
  
    name = CharField(max_length=20, primary_key=True)
    vm_cpu = IntegerField(default=2)
    vm_mem = IntegerField(default=2048)
    create_vm = BooleanField(default=True)
  
    def __unicode__(self):
      return('%s' % (self.name))
  
  class Variable(models.Model):
  
    name = CharField(max_length=10)
    value = CharField(max_length=20)
    host = models.ForeignKey(Host, related_name='vars')
  
    def __unicode__(self):
      return('%s=%s' % (self.name, self.value))
  </code></pre>
  
  <p><strong>Serializer</strong></p>
  
  <pre><code>class VariableSerializer(ModelSerializer):
  
    class Meta:
      model = Variable
  
  class HostSerializer(ModelSerializer):        
  
    vars = VariableSerializer(many=True)
  
    class Meta:
      model = Host
  
    def create(self, validated_data):
  
      # i set a break point here and found vars_data is empty
      vars_data = validated_data.pop('vars')
      host = Host.objects.create(**validated_data)
      for v in vars_data:
          Variable.objects.create(host = host, **v)
  
      return host
  </code></pre>
  
  <p><strong>This is the problem</strong> I found vars_data is an empty list:</p>
  
  <pre><code>  def create(self, validated_data):
  
      # i set a break point here and found vars_data is empty
      vars_data = validated_data.pop('vars')
  </code></pre>
  
  <p>Here's the rest of the code</p>
  
  <p><strong>admin.py</strong></p>
  
  <pre><code>class VariableAdmin(admin.ModelAdmin):
    list_display = ['name', 'value']
  
  class HostAdmin(admin.ModelAdmin):    
    list_display = ['name']
  
  
  admin.site.register(Variable, VariableAdmin)
  admin.site.register(Host, HostAdmin)
  </code></pre>
  
  <p><strong>urls.py</strong></p>
  
  <pre><code>router = DefaultRouter()
  router.register(r'variables', VariableViewSet, base_name='variables')
  router.register(r'hosts', HostViewSet, base_name='hosts')
  
  urlpatterns = [
            url(r'^', include(router.urls)),
            ]
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>class VariableViewSet(ModelViewSet):
    queryset = Variable.objects.all()
    serializer_class = VariableSerializer
  
  class HostViewSet(ModelViewSet):
    queryset = Host.objects.all()
    serializer_class = HostSerializer
  </code></pre>
  
  <p>My test program</p>
  
  <p><strong>post.py</strong></p>
  
  <pre><code>import json
  import requests
  
  file = 'host.json'
  url = 'http://localhost:8001/test_nest/hosts/'
  
  with open(file, 'r') as f:
    j = f.read()
  
  data = json.loads(j)
  
  r = requests.post(url, data = data)
  print r.text
  </code></pre>
  
  <p>And here's the test data </p>
  
  <p><strong>host.json</strong></p>
  
  <pre><code>{
  "name": "host4",
  "vars": [
      {
          "name": "var2-a",
          "value": "a1"
      },
      {
          "name": "var2-b",
          "value": "a2"
      }
  ],
  "vm_cpu": 2,
  "vm_mem": 2048,
  "create_vm": true
  }
  </code></pre>
  
  <p>I'm new to django.  So I'm wondering if it's something simple and obvious.  Did I use the wrong viewset?  Did I post to the wrong URL?  Or I setup the URL structure wrong?</p>