python - Django Tastypie 创建 API 接受 POST 数据但不在数据库中创建任何条目

标签 python django tastypie

我的问题正是它的主题所说的:
如何创建 Django Tastypie API,它接受 POST 数据,对其进行一些处理并返回一些 HTTP 响应,但不在数据库中创建任何条目。


例如,此示例 API 资源:

class NextNumberResource(ModelResource):
class Meta:
    resource_name = 'next_number'
    detail_allowed_methods = []
    list_allowed_methods = ['post']


def obj_create(self, bundle, **kwargs):

    #raise CustomBadRequest(code = "code ={c}".format(c=int(bundle.data["number"])*2))
    next_number = int(bundle.data["number"]) * 2
    data = json.dumps({"next_number":next_number})
    return HttpResponse(data, content_type='application/json', status=200)

我收到以下错误:
{"error_message": "'HttpResponse' 对象没有属性 'pk'"}

最佳答案

我认为最好在dispatch_*方法(例如dispatch_list)中处理这个请求。

例如here .

说明:如果您处理不创建任何实例的发布请求,则必须在 tastypie 的 std 工作流程之前处理它。

关于python - Django Tastypie 创建 API 接受 POST 数据但不在数据库中创建任何条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32127874/

相关文章:

Python Celery 收到类型未注册的任务 - 导入应用程序错误

python - 如何避免将每个变量显式设置为 False?

python - django 具有多个数据库和用户外键

python - 如何在 django 中使尾部斜杠可选

django - 在 Ubuntu 中使用 nginx 和 uwsgi 运行 Django 的问题

Django · Tastypie

python - 将相关资源与 TastyPie 相结合

Python - 在元组集中定位元素

python - pygtk的Unicode问题

django - tastypie 注册问题