我有兩個型號Django的REST框架模型外鍵
class Order(models.Model):
fields...
class OrderItem(models.Model):
fiels...
order = models.ForeignKey(Order, related_name='items')
和我有兩個串行:
class CreateOrderItemSerializer(serializers.ModelSerializer):
class Meta:
model = OrderItem
resource_name = 'order-item'
fields = ('order', 'count')
.....
class OrderSerializer(serializers.ModelSerializer):
items = CreateOrderItemSerializer(many=True)
class Meta:
model = Order
resource_name = 'order'
fields = ('id','items')
我張貼以便與項目數組是這樣的:
{
'ordername': 'foo',
'items': [{
'name': 'foo1',
},{
'name': 'foo2',
},
]
}
但我有錯誤:
"order":["This field is required."]
我該怎麼先創建訂單,然後用這個orderid創建項目?