当我尝试通过我的项目 API 使用外键(在本例中为 2)创建一个对象时,tastepie 也尝试创建相关对象(此处的订单和参与者):
class ParticipationResource(ModelResource):
order = fields.ForeignKey(Order, 'order', full=True,)
participant = fields.ForeignKey(UserProfile, 'participant', full=True)
class Meta:
authorization = Authorization()
queryset = Participation.objects.all()
resource_name = 'participation'
fields = ['id', 'order', 'participant', 'products', 'created_at', 'modified_at']
filtering = {
'participant': ALL
}
detail_allowed_methods = ['get', 'post', 'put', 'delete',]
always_return_data = True
发布的数据:
{
"order": {
"id":"1",
"resource_uri":"/api/v1/order/1/"
...
},
"participant":{
"id":"1",
"resource_uri":"/api/v1/participant/1/"
...
},
"products":[]
}
错误消息(network_id 是用户配置文件模型上的外键):
"error_message": "app_user_profile.network_id may not be NULL",
如您所见,我在 POST 请求中发送了完整的对象,我只尝试了 resource_uri 并且效果很好,问题是我需要完整的对象进行客户端渲染(我使用的是 Backbone,模型是自动渲染)。那我该怎么办?有没有办法告诉tastepie不要创建相关对象?