title: Django: Prefetch_related on nested attributes
body: <p>I need to list all my devices. To do this I use a prefetch related to reduce the amount of queries. But one of them is consuming to much time.. I wonder if it can't go better.</p>
  
  <p>I will start with the model constructions: I want a list of devices.</p>
  
  <pre><code>class Device(models.Model):
      name = models.CharField(max_length=250, null=True, blank=True)
  
  class GatewayDevice(models.Model):
      gateway = models.ForeignKey(
          Gateway, on_delete=models.CASCADE, related_name="devices"
      )
      device = models.ForeignKey(
          Device, on_delete=models.CASCADE, related_name="gatewaydevices"
      )
  </code></pre>
  
  <p>In the real code the device model is larger, but that code is irrelevant. As you can see, a device has some gatewaydevices (which is a model between gateway and device)</p>
  
  <p>So in my list of devices, I want for every device, the linked gateway.</p>
  
  <p>This is my view:</p>
  
  <pre><code>class AdminDeviceView(GenericAPIView):
      def get_permissions(self):
          return IsAuthenticated()
  
      # noinspection PyMethodMayBeStatic
      def get_serializer_class(self):
          return AdminDeviceInfoSerializer
  
      @swagger_auto_schema(
          responses={
              200: openapi.Response(
                  _("Successfully fetched all data from devices."),
                  AdminDeviceInfoSerializer,
              )
          }
      )
  
      def get(self, request):
      """
      GET the data from all the devices.
      """
  
      devices = (
          Device.objects.filter()
          .all()
          .prefetch_related(
              "site__users",
              "software_update_history",
              "supplier",
              Prefetch(
                  "gatewaydevices",
                  queryset=GatewayDevice.objects.filter(end_date=None)
                  .order_by()
                  .distinct()
                  .prefetch_related("gateway"),
              ),
          )
      )
  
      serializer_class = self.get_serializer_class()
      serializer = serializer_class(devices, many=True)
      devices_data = serializer.data
  
      return Response(
          {"total": devices.count(), "items": devices_data}, status=status.HTTP_200_OK
      )
  </code></pre>
  
  <p>This is the part of the serializer that is important:</p>
  
  <pre><code>@staticmethod
  def get_gateway(device):
      return (
          GatewaySimpleSerializer(device.gatewaydevices.gateway).data
          if device.gatewaydevices.gateway
          else None
      )
  </code></pre>
  
  <p>I tried different approaches. This one is the current one.. Now I get this error:</p>
  
  <pre><code>AttributeError: 'RelatedManager' object has no attribute 'gateway'
  </code></pre>