Profile Django views with management command using cProfile, LineProfiler
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Profiling django view functions

The default profiler is cPython.

Profiling is done via management command.

TODO: Currently class-based views are not supported


Download and run
python install

For flexibility, it's a good practice to use module for local settings.

Register app in your



Set up the following options according to your project:

# In case you use OneToOne with # you need to specify it explicitly. # If this option is not set, django.contrib.auth.models.User # is used by default PROFILER_USER_MODEL = 'yourapp.models.User' PROFILER_LIMIT = 30 # by default output first 30 stats cProfile results PROFILER_QUERIES_LIMIT = 3 # output by default 3 slowest queries


NOTE: If your view is decorated, LineProfiler will output stats for the top decorator, so it's recomended temporarily disable decorators if you want to profile your view with LineProfiler

Assuming you have a view function named user_details(request, translator_id) in the app account:

./ profile_view --prefix=account.views user_details:7 --profiler=cProfile,LineProfiler

Here 7 is an argument to user_details function.

For help:

./ help profile_view

If you want to use LineProfiler, install it:

pip install line_profiler

If you want to store contents in a file:

yes y | ./ profile_view --prefix=account.views user_details:7 --profiler=cProfile,LineProfiler > user_details_7_profiling_results.txt