Question

How do I customise and place data in the response to a POST request? At first I thought I should place my code in obj_create but it looks like create_response is the place I should place my code.

My resource looks like this:

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)

However I get a HTTP/1.0 204 NO CONTENT error; when I remove the create_response function, it works as normal. Shouldn't calling super on the function I'm overriding have no effect?

Was it helpful?

Solution

You must return response.

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

But I would use dehydrate method for that:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top