~ubuntu-branches/ubuntu/quantal/python-django/quantal-security

« back to all changes in this revision

Viewing changes to docs/topics/db/sql.txt

  • Committer: Bazaar Package Importer
  • Author(s): Jamie Strandboge
  • Date: 2010-10-12 11:34:35 UTC
  • mfrom: (1.2.7 upstream)
  • mto: This revision was merged to the branch mainline in revision 30.
  • Revision ID: james.westby@ubuntu.com-20101012113435-9lnsrh0i3mxozbt0
Tags: upstream-1.2.3
ImportĀ upstreamĀ versionĀ 1.2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
.. _topics-db-sql:
2
 
 
3
1
==========================
4
2
Performing raw SQL queries
5
3
==========================
6
4
 
7
5
.. currentmodule:: django.db.models
8
6
 
9
 
When the :ref:`model query APIs <topics-db-queries>` don't go far enough, you
 
7
When the :doc:`model query APIs </topics/db/queries>` don't go far enough, you
10
8
can fall back to writing raw SQL. Django gives you two ways of performing raw
11
9
SQL queries: you can use :meth:`Manager.raw()` to `perform raw queries and
12
10
return model instances`__, or you can avoid the model layer entirely and
116
114
 
117
115
    >>> people = Person.objects.raw('SELECT id, first_name FROM myapp_person')
118
116
 
119
 
The ``Person`` objects returned by this query will be :ref:`deferred
120
 
<queryset-defer>` model instances. This means that the fields that are omitted
121
 
from the query will be loaded on demand. For example::
 
117
The ``Person`` objects returned by this query will be deferred model instances
 
118
(see :meth:`~django.db.models.QuerySet.defer()`). This means that the fields
 
119
that are omitted from the query will be loaded on demand. For example::
122
120
 
123
121
    >>> for p in Person.objects.raw('SELECT id, first_name FROM myapp_person'):
124
122
    ...     print p.first_name, # This will be retrieved by the original query
269
267
-----------------------
270
268
 
271
269
``connection`` and ``cursor`` mostly implement the standard `Python DB-API`_
272
 
(except when it comes to :ref:`transaction handling <topics-db-transactions>`).
 
270
(except when it comes to :doc:`transaction handling </topics/db/transactions>`).
273
271
If you're not familiar with the Python DB-API, note that the SQL statement in
274
272
``cursor.execute()`` uses placeholders, ``"%s"``, rather than adding parameters
275
273
directly within the SQL. If you use this technique, the underlying database