Profiling for Django + Tastypie

What is the best tool for profiling a Django application using Tastypie (all answers or JSON, so django_toolbar is not suitable in this case)?

+3
source share
4 answers

One potential solution is to create a view that simply displays TastyPie's responses as HTML. This will allow django-debug-toolbar to output the correct profiling data. Below is a pretty quick and dirty try.

In urls.py:

Just add the following line to url templates, but make sure it is enabled only if debugging is enabled.

(r'^api_profile/(?P<resource>.*)$', 'common.views.api_profile')

In general /views.py:

Put this view anywhere, I put it in my regular application.

from django.shortcuts import render_to_response
# Import the tastypie.api.Api object with which your api resources are registered. 
from apps.api.urls import api

def api_profile(request, resource):
    """ Allows easy profiling of API requests with django-debug-toolbar. """
    context = {}
    resource = resource.strip('/')
    resource = api.canonical_resource_for(resource)
    obj_list = resource.wrap_view('dispatch_list')(request)
    response = resource.create_response(request, obj_list)
    context['api_response'] = response
    return render_to_response('common/api_profile.html', context)

In the / common / api _profile.html templates:

.

<body>
    {{api_response}}
</body>

'/api_profile/a_resource/' django-debug a_resource. , . "api_profile/posts/? Limit = 8 & offset = 16".

+1

β†’ https://djangosnippets.org/snippets/2126/

,

Set this by adding it to MIDDLEWARE_CLASSES. It is active if you are logged in as root or whenever .DEBUG settings are true. To use it, pass 'profile = 1' as the GET or POST parameter for any HTTP request.

It will create an interactive HTML code showing completed requests, time, methods ... you should try it!

0
source

All Articles