Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save igniteflow/a1b438d5aba485cc2709 to your computer and use it in GitHub Desktop.

Select an option

Save igniteflow/a1b438d5aba485cc2709 to your computer and use it in GitHub Desktop.
Add content_as_python attribute to the APIClient.get() returned response object
import json
from rest_framework.test import APIClient
def get(self, *args, **kwargs):
response = super(APIClient, self).get(*args, **kwargs)
try:
response.content_as_python = json.loads(response.content)
except ValueError as e:
response.content_as_python = e
return response
APIClient.get = get
api_client = APIClient()
response = api_client.get('/api/spam/)
print(response.content_as_python) # JSON object has been loaded to python
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment