Moh*_*Ali 2 django ajax tastypie
我发送一个POST来创建一个新的简单Resource(不是ModelResource),并且可行.
我的问题是如何将创建的资源bundle属性恢复到ajax响应?
这是资源示例:
class MyResource(Resource):
x = fields.CharField(attribute='x')
y = fields.CharField(attribute='y')
class Meta:
resource_name = 'myresource'
object_class = XYObject
authorization = Authorization()
def obj_create(self, bundle, request=None, **kwargs):
x = bundle.data["x"]
x = bundle.data["y"]
bundle.obj = XYObject(x, y)
return bundle
Run Code Online (Sandbox Code Playgroud)
这是POST请求
$.ajax({
type: "POST",
url: '/api/v1/myresource/',
contentType: 'application/json',
data: data,
dataType: 'json',
processData: false,
success: function(response)
{
//get my resource here
},
error: function(response){
$("#messages").show('error');
}
});
Run Code Online (Sandbox Code Playgroud)
你可以添加always_return_data = True到你的Meta.然后,您将获得202序列化数据,而不是正常数据201.
以下是文档:http://django-tastypie.readthedocs.org/en/latest/resources.html#always-return-data