8

I would like to learn how to get many to many intermediate table's serializer data by whole model , not only by id.

#this is my model class
class ProductMaterial(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    material = models.ForeignKey(Material, on_delete=models.CASCADE)
    material_rate = models.FloatField(blank=True, null=True)
    material_price = models.FloatField(blank=True, null=True)



#serializer
class ProductMaterialSerializer(serializers.ModelSerializer):
       class Meta:
           model = ProductMaterial
           fields = '__all__'

This returns:

    {
        "id": 1,
        "material_rate": 0.3,
        "material_price": 6.7,
        "product": 186,
        "material": 7
    },
   {
        "id": 2,
        "material_rate": 0.7,
        "material_price": 1.7,
        "product": 186,
        "material": 8
    },

Problems:

  1. First problem is it duplicates data because of many to many table
  2. I want to see my product and material model fields too.

My target:

{
    "id": 1,
    "product": {
                 "name" : "abcd",
                 "date" : "01.01.2018"

                },
    "material": [
                  {
                     "id" : 7,
                     "material_rate" : 0.3,
                     "material_price" : 6.7,

                  },
                  {
                     "id" : 8,
                     "material_rate" : 0.7,
                     "material_price" : 1.7,

                  },
               ]
},

Solution 2:

I have implemented this solution -> https://stackoverflow.com/a/45834689/5491260 and it helped me.

adnan kaya
  • 531
  • 3
  • 13

1 Answers1

5

From the doc,

The depth option should be set to an integer value that indicates the depth of relationships that should be traversed before reverting to a flat representation.

So, use depth=1 in Meta class

class ProductMaterialSerializer(serializers.ModelSerializer):
       class Meta:
           model = ProductMaterial
           fields = '__all__'
           depth = 1
JPG
  • 82,442
  • 19
  • 127
  • 206
  • Thank you it solved my 2. problem. Could you share with me how can i solve "duplication" problem ? – adnan kaya Sep 12 '18 at 12:47
  • What is the duplicate data here? **`product`**? – JPG Sep 12 '18 at 12:49
  • yes **product** duplicates. As you see on my target I want to get like that format. – adnan kaya Sep 12 '18 at 12:50
  • In my model **a** `Product` can have **many** `Material`s and **a** `Material` can be in **many** `Product`s and every `Material` has own `rate, price` in a specific `Product`, I'm expecting to get my data format like on **my target** – adnan kaya Sep 12 '18 at 13:00
  • @JPG `Product` is a FK. I think `ProductMaterial` can only have at most 1 `Product` associated with it (not 1 or more). – slider Sep 12 '18 at 14:44
  • @adnankaya As per the current model definition, you can't obtain the desired output (*Problem no. 2*). Because, that's how its behaves – JPG Sep 13 '18 at 03:44