title: How can I use a serializer to retrieve data from a django model in a nested way?
body: <p>I have a django model with 3 fields: &quot;product&quot;, &quot;price_1&quot; and &quot;price_2&quot;.</p>
  <p>I would like to serialize this model in a way that the result would be:</p>
  <pre><code>{
     &quot;product&quot;: &quot;some product&quot;,
     &quot;price&quot;: {
        &quot;price_1&quot;: 15.00,
        &quot;price_2&quot;: 25.00
     }
  }
  </code></pre>
  <p>I was able to reach this result by creating a model for the price and connecting it to the product model with a foreign key.</p>
  <pre><code>#Models
  
  class Product(models.Model):
     product = models.CharField()
     price = models.ForeignKey(Price)
  
  class Price(models.Model):
     price_1 = models.FloatField()
     price_2 = models.FloatField()
  
  #Serializers
  
  class PriceSerializer(serializers.ModelSerializer):
     class Meta:
        model = Price
        fields = ['price_1', 'price_2']
  
  class ProductSerializer(serializers.ModelSerializer):
     price = PriceSerializer()
  
     class Meta:
        model = Product
        fields = [
           &quot;product&quot;, 
           &quot;price&quot;,
        ]
  </code></pre>
  <p>It worked, but it's very ineffective. Is there a way for me to achieve the same result, but having only one model such as:</p>
  <pre><code>class Product(models.Model):
     product = models.CharField()
     price_1 = models.FloatField()
     price_2 = models.FloatField()
  </code></pre>
  <p>And it would be even better if I could have only the numbers without the word &quot;price&quot;:</p>
  <pre><code>{
     &quot;product&quot;: &quot;some product&quot;,
     &quot;price&quot;: {
        1: 15.00,
        2: 25.00
     }
  }
  </code></pre>
  <p>Although the second result is better, I'd already be very happy if I could achieve the first one using only one model.</p>
  <p>Thanks in advance!</p>
  <p>Edit:</p>
  <p>This is the view I'm using:</p>
  <pre><code>class ProductReadAPIView(generics.RetrieveAPIView):
      lookup_field = 'pk'
      serializer_class = ProductSerializer
  
      def get_queryset(self):
          return Product.objects.all()
  </code></pre>