Skip to main content

Debugging tools for django

Project description


Simple debug utils for Django apps to aid with test-driven development.

Build Status Test Coverage


$ pip install dj.debug

For development install

$ pip install -e
$ pip install dj.debug[test]

Initializing the builtin object

The utils can be imported directly, but the better way is to use _trace builtin functionality.

To activate this call the following, anywhere in your code - eg. in your test settings

>>> from dj.debug import trace_debug

>>> trace_debug()

Once this is done, you will have a _trace object available anywhere in your environment.

>>> _trace
<dj.debug.builtin.Trace object at ...>

You can specify the name of the builtin

>>> trace_debug("_t")
>>> _t
<dj.debug.builtin.Trace object at ...>

Using the builtin object has the advantage that it won’t be recognized by linters if you accidentally leave it in your code.

Usage - pdb

The _t object provides quick access to pdb

>>> _t.pdb.set_trace()
> <stdin>(1)<module>()->None

Usage - trace debugging

If a line of code that you want to debug is hit many times prior to the point at which you want to debug it you can use the _t object’s debug flag

>>> def commonly_hit_code(*args, **kwargs):
...     if _t.debug: _t.pdb.set_trace()

>>> def some_other_code(*args, **kwargs):
...     # we only want to debug after this point
...     _t.debug = True
...     something_which_triggers_commonly_hit_code()

Usage - sql debugging

This tools is useful for finding non-performant code in Django. By tracing the sql that is being run in blocks of code, you can find and fix querysets that trigger too many queries, are too complex, have overly large results, etc. You can also use the output in Django’s dbshell to analyze, improve and add indeces where appropriate.

>>> with _t.debug_sql():
...     trigger_some_orm_action()

dj.debug usage



Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj.debug-0.0.2.tar.gz (5.8 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page