title: How to serialize the foreign key field in django rest framework
body: <p>I work on a project with DRF where I'm getting serializer data as follows which is absolutely fine:</p>
  <pre><code>{
      &quot;message&quot;: &quot;Updated Successfully&quot;,
      &quot;status&quot;: 200,
      &quot;errors&quot;: {},
      &quot;data&quot;: {
          &quot;id&quot;: 8,
          &quot;user&quot;: 2,
          &quot;item&quot;: 1,
          &quot;quantity&quot;: 4,
          &quot;created_at&quot;: &quot;2021-08-11T13:49:27.391939Z&quot;,
          &quot;updated_at&quot;: &quot;2021-08-11T13:51:07.229794Z&quot;
      }
  }
  </code></pre>
  <p>but I want to get as follows:</p>
  <pre><code>{
      &quot;message&quot;: &quot;Updated Successfully&quot;,
      &quot;status&quot;: 200,
      &quot;errors&quot;: {},
      &quot;data&quot;: {
          &quot;id&quot;: 8,
          &quot;user&quot;: &quot;user name&quot;,
          &quot;item&quot;: &quot;product name&quot;,
          &quot;price: &quot;3.44&quot;,
          &quot;quantity&quot;: 4,
          &quot;created_at&quot;: &quot;2021-08-11T13:49:27.391939Z&quot;,
          &quot;updated_at&quot;: &quot;2021-08-11T13:51:07.229794Z&quot;
      }
  }
  </code></pre>
  <p>I tried using DRF <code>RelatedField</code> and <code>PrimaryKryRelatedField</code> but in all these cases I need to make corresponding fields as <code>read_only=True</code> which I want to skip.</p>
  <p>I also tried with <code>depth = 1</code> which gives entire details</p>
  <p>My Model:</p>
  <pre><code>class Cart(models.Model):
      user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
      item = models.ForeignKey(Product, on_delete=models.SET_NULL, null=True, blank=True)
      quantity = models.IntegerField(null=False)
      created_at = models.DateTimeField(auto_now_add=True)
      updated_at = models.DateTimeField(auto_now=True)
  
      def __str__(self):
          return &quot;{} - {} - {} - {} - {}&quot;.format(self.user,
                                                 self.item,
                                                 self.quantity,
                                                 self.created_at,
                                                 self.updated_at)
  </code></pre>
  <p>My serializer:</p>
  <pre><code>class CartSerializer(serializers.ModelSerializer):
      user = serializers.PrimaryKeyRelatedField(source='user.first_name' ,queryset=User.objects.all(), many=False)
      class Meta:
          model = Cart
          fields = ['id', 'user', 'item', 'quantity', 'created_at', 'updated_at']
  </code></pre>
  <p>My View:</p>
  <pre><code>class CartViewSet(viewsets.ModelViewSet):
      queryset = Cart.objects.all().order_by('id')
      serializer_class = CartSerializer
  
      def list(self, request, *args, **kwargs):
          queryset = self.get_queryset()
          serializer = self.serializer_class(
              queryset, context={'request': request}, many=True)
          data = serializer.data
          context = {
              'message': 'Listed Successfully',
              'count': queryset.count(),
              'errors': False,
              'data': data,
          }
          return Response(context, status=status.HTTP_200_OK)
  
      def create(self, request, *args, **kwargs):
          if not request.data:
              return Response(&quot;No data&quot;, status=status.HTTP_400_BAD_REQUEST)
          serializer = self.get_serializer(data=request.data)
          if serializer.is_valid():
              self.perform_create(serializer)
          context = {
              'message': 'Created Successfully',
              'status': status.HTTP_201_CREATED,
              'errors': serializer.errors,
              'data': serializer.data,
          }
          return Response(context)
  
      def update(self, request, *args, **kwargs):
          partial = kwargs.pop('partial', False)
          instance = self.get_object()
  
          serializer = self.get_serializer(
              instance, data=request.data, partial=partial)
          if serializer.is_valid():
                  self.perform_update(serializer)
          context = {
              'message': 'Updated Succesfully',
              'status': status.HTTP_200_OK,
              'errors': serializer.errors,
              'data': serializer.data,
          }
          return Response(context)
  
      def destroy(self, request, *args, **kwargs):
          instance = self.get_object()
          self.perform_destroy(instance)
          context = {
              'message': 'Deleted Succesfully',
              'status': status.HTTP_204_NO_CONTENT,
              'errors': False,
          }
          return Response(context)
          
  
      @action(methods=['get'], detail=False, url_path='checkout/(?P&lt;userId&gt;[^/.]+)', url_name='checkout')
      def checkout(self, request, *args, **kwargs):
  
          try:
              user = User.objects.get(pk=int(kwargs.get('userId')))
          except Exception as e:
              return Response(status=status.HTTP_404_NOT_FOUND, data={'Error': str(e)})
  
          cart_helper = CartHelper(user)
          checkout_details = cart_helper.prepare_cart_for_checkout()
  
          if not checkout_details:
              return Response(status=status.HTTP_404_NOT_FOUND,
                              data={'error': 'Cart of user is empty.'})
  
          return Response(status=status.HTTP_200_OK, data={'checkout_details': checkout_details})
  </code></pre>
  <p>Please if anyone can help, it would be greatly appreciated.
  Thanks</p>