mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
[4.2.x] Improved examples in docs about raw SQL queries.
Regression in14459f80ee
. Backport of498195bda4
from main
This commit is contained in:
@@ -121,11 +121,13 @@ had ``Person`` data in it, you could easily map it into ``Person`` instances:
|
|||||||
.. code-block:: pycon
|
.. code-block:: pycon
|
||||||
|
|
||||||
>>> Person.objects.raw(
|
>>> Person.objects.raw(
|
||||||
... """SELECT first AS first_name,
|
... """
|
||||||
... last AS last_name,
|
... SELECT first AS first_name,
|
||||||
... bd AS birth_date,
|
... last AS last_name,
|
||||||
... pk AS id,
|
... bd AS birth_date,
|
||||||
... FROM some_other_table"""
|
... pk AS id,
|
||||||
|
... FROM some_other_table
|
||||||
|
... """
|
||||||
... )
|
... )
|
||||||
|
|
||||||
As long as the names match, the model instances will be created correctly.
|
As long as the names match, the model instances will be created correctly.
|
||||||
@@ -175,8 +177,9 @@ fields that are omitted from the query will be loaded on demand. For example:
|
|||||||
|
|
||||||
>>> for p in Person.objects.raw("SELECT id, first_name FROM myapp_person"):
|
>>> for p in Person.objects.raw("SELECT id, first_name FROM myapp_person"):
|
||||||
... print(
|
... print(
|
||||||
... p.first_name, p.last_name # This will be retrieved by the original query
|
... p.first_name, # This will be retrieved by the original query
|
||||||
... ) # This will be retrieved on demand
|
... p.last_name, # This will be retrieved on demand
|
||||||
|
... )
|
||||||
...
|
...
|
||||||
John Smith
|
John Smith
|
||||||
Jane Jones
|
Jane Jones
|
||||||
|
Reference in New Issue
Block a user