title: Django Rest Framework, group_by queries using .values(), and embedding a related record
body: <p>I can't figure out how to embed a related record into a Django Rest Framework response when using .values() on a Django query set to return grouped by aggregated values. Sample Models, Serializers, and Viewset included below along with current response and a sample desired response.</p>
  <p>Each Trade has one security. Multiple trades can exist for a security. I have a need to display Trades individually and also grouped by security. When grouping by security, I want to return the embedded security in the response along with the aggregate values for the associated trades, but so far I can only get the security id returned. Any attempt to get the security itself in the response results in an exception <code>'int' object has no attribute 'pk'</code></p>
  <p>Trade model</p>
  <pre><code>class Trade(models.Model):
      security = models.ForeignKey('Security')
      ticker = models.CharField(
          max_length=128,
          null=True,
          blank=True,
          db_index=True,
      )
      shares = models.FloatField(db_index=True)
      value_usd = models.FloatField(db_index=True)
      days_to_trade = models.FloatField(db_index=True)
      trade_date = models.DateField(
          db_index=True,
          null=True,
          blank=True,
      )
  </code></pre>
  <p>Security model</p>
  <pre><code>class Security(models.Model):
      name = models.CharField(
          db_index=True,
          blank=True,
          null=True,
      )
      ipo_date = models.DateField(
          null=True,
          blank=True
      )
  </code></pre>
  <p>Trade View</p>
  <pre><code>class TradeViewSet(viewsets.ModelViewSet):
      def get_queryset(self):
          // param and filter setup/defaults snipped for clarity
          ...
          qs = Trade.objects.filter(**qs_filter)
          // if group_by param is passed in, then group the trades by ticker/security and return aggregate sums for value, shares, and days to trade
          if group_by:
              qs = qs.values('ticker', 'security_id').annotate(value_usd=Sum('value_usd'), shares=Sum('shares'), days_to_trade=Sum('days_to_trade'), num_trades=Count('security')).order_by('ticker')
              self.serializer_class = TradeByTickerSerializer
          return qs
  
      serializer_class = TradeSerializer
  </code></pre>
  <p>Trade serializer</p>
  <pre><code>class TradeSerializer(serializers.ModelSerializer):
      security = SecuritySerializer(many=False, read_only=True)
      class Meta:
          model = Trade
          fields = [
              'id',
              'security',
              'trade_date',
              'ticker',
              'shares',
              'value_usd',
              'days_to_trade',
          ]
          depth = 1
  </code></pre>
  <p>TradeByTicker serializer</p>
  <pre><code>class TradeByTickerSerializer(serializers.ModelSerializer):
      num_trades = serializers.IntegerField(read_only=True)
      // security = SecuritySerializer(many=False, read_only=True) // this gives an exception 
      class Meta:
          model = Trade
          fields = [
              // 'security', // I tried this but get an exception
              'security_id',
              'ticker',
              'shares',
              'value_usd',
              'days_to_trade',
              'num_trades',
          ]
  </code></pre>
  <p>Current output</p>
  <pre><code>{
      &quot;results&quot;: [
          {
              &quot;securityId&quot;: 123,
              &quot;ticker&quot;: &quot;ABC-US&quot;,
              &quot;shares&quot;: 790611.154356048,
              &quot;valueUsd&quot;: 15598758.0754448,
              &quot;daysToTrade&quot;: 7.2460672754406,
              &quot;numTrades&quot;: 1
          },
          {
              &quot;securityId&quot;: 456,
              &quot;ticker&quot;: &quot;DEF-US&quot;,
              &quot;shares&quot;: 1116548.93406872,
              &quot;valueUsd&quot;: 29041437.7751273,
              &quot;daysToTrade&quot;: 6.6811609336384,
              &quot;numTrades&quot;: 3
          },
          {
              &quot;securityId&quot;: 789,
              &quot;ticker&quot;: &quot;HIJ-US&quot;,
              &quot;shares&quot;: 979099.946772097,
              &quot;valueUsd&quot;: 16360760.1105617,
              &quot;daysToTrade&quot;: 6.50616625094424,
              &quot;numTrades&quot;: 2
          },
          ...
  ]
  </code></pre>
  <p>Desired output</p>
  <pre><code>{
      &quot;results&quot;: [
          {
              &quot;security&quot;: {
                  &quot;id&quot;: 123,
                  &quot;name&quot;: &quot;Another Boring Company&quot;,
                  &quot;ipoDate&quot;: &quot;1988-12-23&quot;
              },
              &quot;ticker&quot;: &quot;ABC&quot;,
              &quot;shares&quot;: 790611.154356048,
              &quot;valueUsd&quot;: 15598758.0754448,
              &quot;daysToTrade&quot;: 7.2460672754406,
              &quot;numTrades&quot;: 1
          },
          {
              &quot;security&quot;: {
                  &quot;id&quot;: 456,
                  &quot;name&quot;: &quot;Def Jam Records&quot;,
                  &quot;ipoDate&quot;: &quot;2001-04-24&quot;
              },
              &quot;ticker&quot;: &quot;DEF&quot;,
              &quot;shares&quot;: 1116548.93406872,
              &quot;valueUsd&quot;: 29041437.7751273,
              &quot;daysToTrade&quot;: 6.6811609336384,
              &quot;numTrades&quot;: 3
          },
          {
              &quot;security&quot;: {
                  &quot;id&quot;: 789,
                  &quot;name&quot;: &quot;Hijinx Corp&quot;,
                  &quot;ipoDate&quot;: &quot;1999-12-31&quot;
              },
              &quot;ticker&quot;: &quot;HIJ&quot;,
              &quot;shares&quot;: 979099.946772097,
              &quot;valueUsd&quot;: 16360760.1105617,
              &quot;daysToTrade&quot;: 6.50616625094424,
              &quot;numTrades&quot;: 2
          },
          ...
  ]
  </code></pre>