Created
August 27, 2012 18:11
-
-
Save ptone/3490967 to your computer and use it in GitHub Desktop.
Using djangorestframework to get JSON given just a resource outside of a view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpRequest | |
from djangorestframework.views import ListOrCreateModelView | |
from djangorestframework.response import Response | |
from djangorestframework import status | |
from djangorestframework.renderers import JSONRenderer | |
from colorpicks.resources import ColorResource | |
resource_view = ListOrCreateModelView(resource=ColorResource) | |
dummy_request = HttpRequest() | |
response_obj = resource_view.get(dummy_request) | |
response = Response(status.HTTP_200_OK, response_obj) | |
jsonr = JSONRenderer(resource_view) | |
filtered_content = resource_view.filter_response(response_obj) | |
json_string = jsonr.render(filtered_content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment