title: Problem with writing a django rest serializer
body: <p>I'm trying to write a Django Rest Framework serializer. It is supposed to take in a simple primitive data and return data with some of the related objects data. The model I'm serializing is Model_C</p>
  
  <p>Here are the models:</p>
  
  <pre class="lang-py prettyprint-override"><code>class ModelA(models.Model):
      name = models.Charfield(max_length=16)
      attr1 = models...
      ...
  
  class ModelB(models.Model):
      name = models.CharField(max_length=16)
      attr1 = models...
      ...
  
  class ModelC(models.Model):
      model_a = models.ForeignKey(ModelA)
      model_b = models.ForeignKey(ModelB)
      text = models.CharField(max_length=32)
      date = models.DateTimeField()
  </code></pre>
  
  <p>Here is the api view:</p>
  
  <pre class="lang-py prettyprint-override"><code>class ModelCApiView(RetrieveUpdateDestroyAPIView):
      serializer_class = ModelCSerializer
  
      def get_queryset(self):
          return self.serializer_class.Meta.model.objects.all()
  
      def post(self, request):
          serializer = self.get_serializer(data=request.data)
          if serializer.is_valid():
              serializer.save()
              return Response(data=serializer.data, status=status.HTTP_201_CREATED)
          else:
              return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  
  </code></pre>
  
  <p>The data fed to the serializer are as follows (with or without the ID):</p>
  
  <pre class="lang-xml prettyprint-override"><code>{'id': '1', 'model_a': 1, 'model_b': 1, 'text': 'some text', 'date': '2019-08-28 08:00:00'}
  </code></pre>
  
  <p>Now, the thing is that the serializer should save the instance and return the serialized model WITH SOME OF THE related data:</p>
  
  <pre class="lang-xml prettyprint-override"><code>{'id': '1', 'model_a': {'id': 1, 'name': 'some model a name'}, 'model_b': {'id': 1, 'name': 'some model b name', 'attr1': 'some attr value'}, 'text': 'some text', 'date': '2019-08-28 08:00:00'}
  </code></pre>
  
  <p>So the only thing that is created is ModelC instance. The api view is not supposed to create ModelA or ModelB instances.</p>
  
  <p>The main problem is with serializing/deserializing objects. I already tried several things:</p>
  
  <ol>
  <li>The most simple</li>
  </ol>
  
  <pre class="lang-py prettyprint-override"><code>class ModelCSerializer(serializers.ModelSerializer):
      class Meta:
          model = ModelC
          fields = "__all__"
  </code></pre>
  
  <p>It properly deserializes the request, saves instance, but returns too little information - only IDs of related objects.</p>
  
  <hr>
  
  <ol start="2">
  <li>I used depth</li>
  </ol>
  
  <pre class="lang-py prettyprint-override"><code>class ModelCSerializer(serializers.ModelSerializer):
      class Meta:
          model = ModelC
          fields = "__all__"
          depth = 1
  </code></pre>
  
  <p>This does not work, as it spits out 500 with error: "Column 'model_a_id' cannot be null".</p>
  
  <hr>
  
  <ol start="3">
  <li>I tried adding to 2. declared serializers:</li>
  </ol>
  
  <pre class="lang-py prettyprint-override"><code>class ModelCSerializer(serializers.ModelSerializer):
      class Meta:
          model = ModelC
          fields = "__all__"
          depth = 1
      model_a = ModelASerializer()
      model_b = ModelBSerializer()
  </code></pre>
  
  <p>This produces error 400, bad request, as it asks me to insert ALL data for model_a and model_b as a dict.</p>
  
  <hr>
  
  <ol start="4">
  <li>I added lines to APIView to serialize data with simple serializer, and then serialize again with more advanced and return more info.</li>
  </ol>
  
  <pre class="lang-py prettyprint-override"><code>    serializer = ModelCSerializer
  
      def post(self, request):
          serializer = self.get_serializer(data=request.data)
          if serializer.is_valid():
              obj = serializer.save()
              serializer = ModelCReadOnlySerializer(instance=obj, data=serializer.data)
              serializer.is_valid()
              return Response(data=serializer.data, status=status.HTTP_201_CREATED)
          else:
              return Response(data=serializer.errors, status=status.HTTP_400_BAD_REQUEST)
  
  
  class ModelCReadOnlySerializer(serializers.ModelSerializer):
      class Meta:
          model = ModelC
          fields = ("id", "model_a", "model_b", "text", "date")
          read_only_fields = fields
          depth = 1
  </code></pre>
  
  <p>This is the closest to the solution as I have been, since it returns almost what I need, except for that it returns ALL data for model_a and model_b, which is way too much.</p>
  
  <hr>
  
  <p>At this point I have other ideas, but I bet they're all terrible. I'd be glad on any hints how to proceed, because I'm this close -> || &lt;- to writing everything ny myself without DRF :-)</p>
  
  <p>Thank you in advance.</p>