如何在POST请求中返回数据(django/tastype)

如何在POST请求中返回数据(django/tastype),django,tastypie,Django,Tastypie,如何在对POST请求的响应中定制和放置数据? 起初我认为我应该把代码放在obj_create中,但看起来create_response是我应该放代码的地方 我的资源如下所示: class TestResource(ModelResource): class Meta: queryset = Test.objects.all() always_return_data = True authentication = Authentication

如何在对POST请求的响应中定制和放置数据? 起初我认为我应该把代码放在
obj_create
中,但看起来
create_response
是我应该放代码的地方

我的资源如下所示:

class TestResource(ModelResource):
    class Meta:
        queryset = Test.objects.all()
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization()

    def create_response(self, request, data, response_class=HttpResponse, **response_kwargs):
        super(TestResource, self).create_response(request, data, response_class, **response_kwargs)

但是我得到一个
HTTP/1.0 204无内容
错误;当我删除
create\u response
功能时,它会正常工作。对我覆盖的函数调用super不应该没有效果吗?

您必须返回响应

def create_response(self, request, data, response_class=HttpResponse, **response_kwargs):
    return super(TestResource, self).create_response(request, data, response_class, **response_kwargs) 
但我会使用脱水方法:

class TestResource(ModelResource):
    class Meta:
        queryset = Test.objects.all()
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization()

    def dehydrate(self, bundle):
        if bundle.request.method == 'POST':
            bundle.data['my_custom_data'] = 'my_data'

        return bundle

您必须返回响应

def create_response(self, request, data, response_class=HttpResponse, **response_kwargs):
    return super(TestResource, self).create_response(request, data, response_class, **response_kwargs) 
但我会使用脱水方法:

class TestResource(ModelResource):
    class Meta:
        queryset = Test.objects.all()
        always_return_data = True
        authentication = Authentication()
        authorization = Authorization()

    def dehydrate(self, bundle):
        if bundle.request.method == 'POST':
            bundle.data['my_custom_data'] = 'my_data'

        return bundle