How to use django-debug-toolbar for django-tastypie?
Here is a middleware I wrote for similar purposes that wraps json in HTML for enabling the debug toolbar and also pretty prints it. Furthermore, it supports binary data. I'm not using tastypie, but I think it should work with that too.
# settings-dev.pyfrom django.http import HttpResponseimport json MIDDLEWARE_CLASSES += ( 'debug_toolbar.middleware.DebugToolbarMiddleware', 'NonHtmlDebugToolbarMiddleware',)class NonHtmlDebugToolbarMiddleware(object): """ The Django Debug Toolbar usually only works for views that return HTML. This middleware wraps any non-HTML response in HTML if the request has a 'debug' query parameter (e.g. http://localhost/foo?debug) Special handling for json (pretty printing) and binary data (only show data length) """ @staticmethod def process_response(request, response): if request.GET.get('debug') == '': if response['Content-Type'] == 'application/octet-stream': new_content = '<html><body>Binary Data, ' \ 'Length: {}</body></html>'.format(len(response.content)) response = HttpResponse(new_content) elif response['Content-Type'] != 'text/html': content = response.content try: json_ = json.loads(content) content = json.dumps(json_, sort_keys=True, indent=2) except ValueError: pass response = HttpResponse('<html><body><pre>{}' '</pre></body></html>'.format(content)) return response
The Django Debug Toolbar's middleware actually has code in it to prevent it being activated for non-html type responses like those returned by TastyPie. What I have done in the past is create a bit of middleware that converts json responses into HTML so the toolbar will be activated and I can count queries etc... It is a bit of a hack but it gets to job done and is easy to turn on/off.
from django.conf import settingsclass JsonAsHTML(object): ''' View a JSON response in your browser as HTML Useful for viewing stats using Django Debug Toolbar This middleware should be place AFTER Django Debug Toolbar middleware ''' def process_response(self, request, response): #not for production or production like environment if not settings.DEBUG: return response #do nothing for actual ajax requests if request.is_ajax(): return response #only do something if this is a json response if "application/json" in response['Content-Type'].lower(): title = "JSON as HTML Middleware for: %s" % request.get_full_path() response.content = "<html><head><title>%s</title></head><body>%s</body></html>" % (title, response.content) response['Content-Type'] = 'text/html' return response
I'm afraid that's not possible. See the accepted answer for a workable solution.
Here's why your approach did not work:
The toolbar does not kick-in because the answer is not in HTML. All other formats can't be "parsed" by the toolbar's middleware to include the Toolbar.
You can add your own tools to show SQL queries though. Take a look at this simple snippet: http://djangosnippets.org/snippets/161/ Or you can use a third party app for this, like django-snippetscream.
For example, you could check if DEBUG is True
and add this info to the "meta" object returned by Tastypie.
Also, take a look at the SQL logging in your console (runserver). Some useful resource for this: http://dabapps.com/blog/logging-sql-queries-django-13/