title: Django Rest Framework model Id field in nested relationship serializer
body: <p>I'm usign Django Rest Framework where I have the following two serializers: </p>
  
  <pre><code>class ServiceSerializer(serializers.ModelSerializer):
      id = serializers.ReadOnlyField()
  
      class Meta:
          model = ServiceType
          fields = ('id', 'serviceName', 'servicePrice')
  
  
  class CompanyShortListSerializer(serializers.ModelSerializer):
  
       services = ServiceSerializer(many=True)
  
       class Meta:
           model = Company
           fields = ( 'id','name','address','cost_per_patient','cost_per_month','renting_fee','services')
  </code></pre>
  
  <p>The ServiceType model looks like this: </p>
  
  <pre><code>class ServiceType(models.Model):
       serviceName = EncryptedCharField(max_length=100, blank=True, verbose_name = "Typ usługi")
       servicePrice = EncryptedFloatField(null=True, blank=True, verbose_name = "Cena usługi", validators = [MinValueValidator(0.1), MaxValueValidator(999)])
       company = models.ForeignKey(Company, related_name = 'services')
  </code></pre>
  
  <p>I would like to update the existing instances by changing the related services (e.g. deleting some of them). To achieve this I'm doing this: </p>
  
  <pre><code>def update(self, instance, validated_data):
      # Updates an exisitng Company with several services 
      instance.name = validated_data['name']
      instance.address = validated_data['address']
      instance.cost_per_patient = validated_data['cost_per_patient']
      instance.renting_fee = validated_data['renting_fee']
      services_data = validated_data['services']
  
      for item in services_data:
          updatedService = ServiceType(
              serviceName = item['serviceName'],
              servicePrice = item['servicePrice'],
              id=item['id'], 
              company=instance)
          updatedService.save()
  
      return instance
  </code></pre>
  
  <p>The problem that I'm facing is that the <code>service['id']</code> field is not provided - which means I get a <code>KeyError 'id'</code> - although I added it explicitly in the <code>ServiceSerializer</code> <code>id</code> field. </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Here's an exemplary request (taken from Chrome) that I'm posting: </p>
  
  <pre><code> { "id":49,"name":"Test 1",
     "address":"Testowa 1",
     "renting_fee":200,
     "cost_per_month":300,
     "cost_per_patient":null,
     "services":[
     {"id":67,"serviceName":"Terapia","servicePrice":100},
     {"id":68,"serviceName":"Terapia par","servicePrice":150},
     {"id":69,"serviceName":"Terapia po angielsku","servicePrice":120}
     ]
   } 
  </code></pre>
  
  <p>What am I doing wrong and how to get the ID of a an object(basically ServiceSerializer) using nested serializers? </p>
  
  <p><strong>EDIT no.2</strong></p>
  
  <p>When I print from the serializer update function I get the following: </p>
  
  <p><code>print(self.data['services'])</code> gives me:</p>
  
  <pre><code>[
   OrderedDict([('id', 67), ('serviceName', u'Terapia'), ('servicePrice', 100.0)]),
   OrderedDict([('id', 68), ('serviceName', u'Terapia par'), ('servicePrice', 150.0)]),
   OrderedDict([('id', 69), ('serviceName', u'Terapia po angielsku'), ('servicePrice', 120.0)]), 
   OrderedDict([('id', 70), ('serviceName', u'Terapia grupowa'), ('servicePrice', 140.0)])
  ]
  </code></pre>
  
  <p>However, <code>print(services_data)</code> gives the following (basically the same, but without the id):</p>
  
  <pre><code>[
  OrderedDict([(u'serviceName', u'Terapia'), (u'servicePrice', 100.0)]),
  OrderedDict([(u'serviceName', u'Terapia par'), (u'servicePrice', 150.0)]),
  OrderedDict([(u'serviceName', u'Terapia po angielsku'), (u'servicePrice', 120.0)])
  ]
  </code></pre>
  
  <p>And the <code>ids</code> get lost ... </p>
  
  <p><strong>EDIT no.3</strong></p>
  
  <p>According to the <a href="http://www.django-rest-framework.org/api-guide/validators/#advanced-default-argument-usage" rel="noreferrer">Django Rest Framework docs</a> If certain field will be shall be in the serializer output representation it should be a <code>HiddenField</code>. This however requires a <code>default</code> value ... I have checked that and indeed using this HiddenField with a default value 'solves' the problem - the ID is there in validated_data. The problem is that the <code>id</code> is not correct. Is there a possibility to set this <code>id</code> to the value of the object sent to the serializer?  </p>