title: Django REST Framework Serializers: Display the latest object of a reverse relationship
body: <p>The default behavior of the ListAPIView (code below) is to serialize all Report objects and the nested Log objects per Report object. What if I only want the latest Log object to be displayed per Report? How do I go about doing that?</p>
  
  <pre><code># models.py
  class Log(models.Model):
      # ...
      report = models.ForeignKey(Report)
      timestamp = models.DateTimeField(default=datetime.datetime.now)
  
  class Report(models.Model):
      code = models.CharField(max_length=32, unique=True)
      description = models.TextField()
  
  
  # serializers.py
  class LogSerializer(serializers.ModelSerializer):
  
      class Meta:
          model = Log
  
  class ReportSerializer(serializers.ModelSerializer):
      log_set = LogSerializer(many=True, read_only=True)
  
      class Meta:
          model = Report
          fields = ('code', 'description', 'log_set')
  
  
  # views.py
  class ReportListView(generics.ListAPIView):
  
      queryset = Report.objects.all()
      serializer_class = ReportSerializer
  </code></pre>
  
  <p>I know I can do this by using a SerializerMethodField, but this can be a potentially expensive operation, since there will be an extra SQL query to retrieve the appropriate Log object for each Report object.</p>
  
  <pre><code>class ReportSerializer(serializers.ModelSerializer):
      latest_log = serializers.SerializerMethodField()
  
      class Meta:
          model = Report
  
      def get_latest_log(self, obj):
          try:
              latest_log = Log.objects.filter(report_id=obj.id).latest('timestamp')
          except Log.DoesNotExist:
              latest_log = None
          return latest_log
  </code></pre>
  
  <p>If I have 1000 report objects, there will be 1000 extra queries if I want to render them all. How do I avoid those extra queries besides using pagination? Can anyone point me to the right direction? Thanks!</p>
  
  <p><strong>EDIT:</strong> Regarding the possible duplicate tag, the link alone provided by Mark did not completely clear up the picture for me. Todor's answer was more clear.</p>