【发布时间】:2014-08-14 16:24:10
【问题描述】:
我有一个如下所示的序列化程序类:
class ShipmentSerializer(serializers.ModelSerializer):
class Meta:
model = Shipment
depth = 1
发货模型有另一个模型Location的外键,称为receiver。目前我可以像这样发布一个嵌套对象:
curl -X POST -H "Content-Type: application/json" -d '{"receiver": {"name": "Bob", "phone": "555-555-5555"}' http://localhost:8000/api/shipments/
但我还需要能够进行平面 POST,指的是接收器的 pk。类似于深度=0:
curl -X POST -H "Content-Type: application/json" -d '{"receiver": 43}' http://localhost:8000/api/shipments/
支持这两种 POST 的最佳方法是什么?
【问题讨论】:
标签: python django django-rest-framework