Skip to main content
 首页 » 编程设计

python之如何在 django tastypie json 上附加 success=true

2025年05月04日21davidwang456

我正在使用 django-tastypie 来实现 restapi,我正在使用 sencha 作为移动客户端。出于某种目的,我需要操纵响应文本。

如下图

form.submit({ 
    success: function() { 
        // The callback function is run when the user taps the 'ok' button 
        form.reset(); 
 
        //Ext.Msg.alert('Thank You', 'Your message has been received', function() { 
        //  form.reset(); 
        //}); 
    } 
}); 

我有如下的 json 响应

{"meta": {"limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 13},        
 "objects": [{"body": "This will prbbly be my lst edited  post.", "id": 1, 
 "pub_date":  "2011-05-22", "resource_uri": "/api/v1/entry/1/", "slug": "another-post",  
 "title": "Another Post"}, {"body": "This will prbbly be my lst post.", "id": 2,  
 "pub_date": "2011-05-22", "resource_uri": "/api/v1/entry/2/", "slug": "another-post",  
 "title": "Another Post"}, {"body": "This will prbbly be my lst edited  post"}]} 

发送成功很重要=> true 如果,success 未定义或不等于 true,那么它将被视为表单提交错误。 如何在 django tastypie json 上附加 success=true

请您参考如下方法:

如果我对您的问题的理解正确,您希望将 {'success': true} 附加到 API 调用的结果中,对吗?如果是这样,您可以覆盖 dehydrate Resource 类上的方法:

def dehydrate(self, bundle): 
    bundle.data['success'] = True 
    return bundle