2

Most of my ModelViewSet's have overriden create method(there is a need to perform additional action when object is created).

class MyModelViewSet(serializers.ModelSerializer):
    def create(self, *args, **kwargs ):
        # some code
        return Response(
            serializer.data,
            status=status.HTTP_201_CREATED,
            headers=headers
        )

And I also want to define a decorator function for those create methods, for example like this:

def post_shower(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        print result
        return result
    return wrapper

Basically I need the response status, headers, new object data. When I run this code, I get

ContentNotRenderedError: The response content must be rendered before it can be accessed.

on the

print result

Any suggestions on how to get the data in decorator ? Thanks

Ostap Khl
  • 183
  • 1
  • 2
  • 9
  • Maybe you can give more detail on which snippet actually throws the error , first or second. In any case, if your view returns a `TemplateResponse` you should call `render()` manually to access it's content as it will not be called automatically [See Manually calling a class based generic view](http://stackoverflow.com/questions/7258912/manually-calling-a-class-based-generic-view) – Moses Koledoye Jan 22 '16 at 10:36

1 Answers1

0

Access new object data through response.data and status through response.status_code. Headers are accessed officially one by one through the Response object's dict-like interface. Or unofficially all at once through response._headers.

Tomas Walch
  • 2,245
  • 1
  • 14
  • 17