Book Image

Django 1.0 Website Development

Book Image

Django 1.0 Website Development

Overview of this book

Django is a high-level Python web framework that was developed by a fast-moving online-news operation to meet the stringent twin challenges of newsroom deadlines and the needs of web developers. It is designed to promote rapid development and clean, pragmatic design and lets you build high-performing, elegant web applications rapidly. Django focuses on automating as much as possible and adhering to the DRY (Don't Repeat Yourself) principle, making it easier to build high-performance web applications faster, with less code. This book will show you how to assemble Django's features and take advantage of its power to design, develop, and deploy a fully-featured web site. It will walk you through the creation of an example web application, with lots of code examples. Specially revised for version 1.0 of Django, the book starts by introducing the main design concepts in Django. Next, it leads you through the process of installing Django on your system. After that, you will start right away on building your social bookmarking application using Django. Various Django 1.0 components and sub-frameworks will be explained during this process, and you will learn about them by example. In each chapter, you will build one or more of the features that are essential in Web 2.0 applications, like user management, tags, and AJAX. You will also learn about good software development practices, such as keeping your application secure, and automating testing with unit tests. By the end of the book, you will have built a fully functional real-life Web 2.0 application, and learned how to deploy it to a production server.
Table of Contents (17 chapters)
Django 1.0 Web Site Development
Credits
About the author
About the reviewer
Preface

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. --'
cursor = connection.cursor()
cursor.execute(query)

If you use a SELECT query, you can retrieve the returned rows using:

rows = cursor.fetchall()

rows is a list of rows. Each row is a list of values that map to columns...