小编Moh*_*Ali的帖子

Tastypie - 如何让obj_create发回新创建的实体?

我发送一个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)

django ajax tastypie

2
推荐指数
1
解决办法
2150
查看次数

标签 统计

ajax ×1

django ×1

tastypie ×1