O'Reilly logo

Django 1.0 Web Site Development by Ayman Hourieh

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Model managers and custom SQL

The Django model and database APIs are very powerful. We used them to construct a variety of query sets throughout the book. Mostly, these APIs will be sufficient for your needs. However, there are times when the task at hand requires raw SQL power. For example, you may want to use SQL aggregate functions such as sum or avg to obtain certain types of information from the database. The database layer of Django does not provide methods that offer similar functionality to aggregate functions at the time of writing. To overcome this, Django enables you to send raw SQL to your database for such special situations.

To send SQL queries to the database in Django, use the following code:

from django.db import connection query = '-- SQL code goes here. --' ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required