Django Debug Toolbar and using select_related

Here's how you can use django-debug-toolbar to debug your Django application.

First install it:

$ pip install django-debug-toolbar

Then add it to INSTALLED_APPS in your local_settings.py like so:

INSTALLED_APPS = INSTALLED_APPS + (
    'debug_toolbar',
)

INTERNAL_IPS = ('ip-that-tries-to-connect', )

To find your INTERNAL_IP execute this in one of your views:

print "IP Address for debug-toolbar: "+ request.META['REMOTE_ADDR']

Now you probably want to reduce some queries by adding selected_related to your query.

Example:

query_set = models.NewsItem.objects.all()

With a template like this:

{% for newsitem in newsitems %}
    {{ newsitem.user.username }}
{% endfor %}


This will result in x * query_set.count() queries. Because for each NewItem, Django will select the user by doing 1 extra query.
To reduce the total queries for this template to 1 query, change query_set to:

query_set = models.NewsItem.objects.select_related('user').all()