Django Serializer for post with related fields

577 Views Asked by At

I have the following Models and Serializer:

class Order(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='orders')
    orderType = models.IntegerField()
    justification = models.CharField("Order justification", max_length=150, default=None, blank=True, null=True)
    date = models.DateTimeField("Order date")
    
    status = OrderType()

class Item(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE, related_name='items')
    product = models.ForeignKey(Product, on_delete=models.CASCADE, related_name='itemsProduct')
    quantity = models.IntegerField("Quantity sale")
    price = models.DecimalField("Sale price", max_digits=15, decimal_places=2)


class OrderItemSerializer(serializers.ModelSerializer):

    items = ItemSerializer()

    class Meta:
        model = Order
        depth = 0
        fields = ["user", "orderType", "justification", "date", "items"]

I want realize a post with a json like this:

{
  "user": 10,
  "orderType": 1,
  "justification": "",
  "date": "2020-09-30T19:11:55.327Z",
  "items": [{
    "quantity": 1,
    "price": 1,
    "order": (need to be the order that i'm posting)
    "product": 1
  },
  {
    "quantity": 1,
    "price": 12,
    "order": (need to be the order that i'm posting)
    "product": 3
  }]
}

But with my serializer the post are:

{
  "user": 10,
  "orderType": 1,
  "justification": "",
  "date": "2020-09-30T19:11:55.327Z",
  "items":{
    "quantity": 1,
    "price": 1,
    "order": 0,
    "product": 1
  }
}

There someway to do a serializer for the post inserting many items with the item.order field automatically setting (like the first json)?

#EDIT 1

To make more clear what I need after the solution of "many=True"... Currently, if i wanna post an Order and an Item, I need first make a post of an order with a json like that:

{
  "user": 10,
  "orderType": 1,
  "justification": "",
  "date": "2020-09-30T19:11:55.327Z"
}

and then I need make a post for item with a json like that:

{
    "quantity": 1,
    "price": 1,
    "order": 1,
    "product": 1
}

in which the field "order" is the pk for Order that django automaticaly generate and return after the first post in my example.

How can I do a post with only one serializer for Ordem that contains many Items, if i don't know previaly the primary key for Ordem? I belive that in my serializer i need override the "create" for set the Order primary key to my items, but i don't know how.

Basicaly, I need that my post work with the following json(without the field "order" in items, because the value for that is generate for django in an Order create):

{
  "user": 10,
  "orderType": 1,
  "justification": "",
  "date": "2020-09-30T19:11:55.327Z",
  "items": [{
    "quantity": 1,
    "price": 1,
    "product": 1
  },
  {
    "quantity": 1,
    "price": 12,
    "product": 3
  }]
}
1

There are 1 best solutions below

3
On

Add many=True to the items = ItemSerializer() row:

items = ItemSerializer(many=True)