mirror of
https://github.com/django/django.git
synced 2025-06-05 03:29:12 +00:00
Fixed #27587 -- Adding examples of QuerySet.query.__str__() to docs.
This commit is contained in:
parent
d62563cbb1
commit
c32a7651a2
@ -473,8 +473,14 @@ Save these changes and start a new Python interactive shell by running
|
|||||||
>>> from polls.models import Choice, Question
|
>>> from polls.models import Choice, Question
|
||||||
|
|
||||||
# Make sure our __str__() addition worked.
|
# Make sure our __str__() addition worked.
|
||||||
>>> Question.objects.all()
|
>>> q = Question.objects.all()
|
||||||
|
>>> q
|
||||||
<QuerySet [<Question: What's up?>]>
|
<QuerySet [<Question: What's up?>]>
|
||||||
|
# Print the SQL used to query the database
|
||||||
|
>>> from pprint import pprint
|
||||||
|
>>> pprint(str(q.query))
|
||||||
|
('SELECT "polls_question"."id", "polls_question"."question_text", '
|
||||||
|
'"polls_question"."pub_date" FROM "polls_question"')
|
||||||
|
|
||||||
# Django provides a rich database lookup API that's entirely driven by
|
# Django provides a rich database lookup API that's entirely driven by
|
||||||
# keyword arguments.
|
# keyword arguments.
|
||||||
|
@ -336,6 +336,19 @@ objects from the database. However, that's far from all there is; see the
|
|||||||
:ref:`QuerySet API Reference <queryset-api>` for a complete list of all the
|
:ref:`QuerySet API Reference <queryset-api>` for a complete list of all the
|
||||||
various :class:`~django.db.models.query.QuerySet` methods.
|
various :class:`~django.db.models.query.QuerySet` methods.
|
||||||
|
|
||||||
|
|
||||||
|
Printing the ``QuerySet`` SQL
|
||||||
|
--------------------------
|
||||||
|
|
||||||
|
When debugging a query, it may be useful to see the SQL generated by a
|
||||||
|
queryset. You may do this by converting the ``query`` attribute to a string.
|
||||||
|
|
||||||
|
>>> q = Entry.objects.all()
|
||||||
|
>>> str(q.query)
|
||||||
|
|
||||||
|
This prints the SQL that is used to query the database. From there, you may
|
||||||
|
looks for unexpected clauses, etc.
|
||||||
|
|
||||||
.. _limiting-querysets:
|
.. _limiting-querysets:
|
||||||
|
|
||||||
Limiting ``QuerySet``\s
|
Limiting ``QuerySet``\s
|
||||||
|
Loading…
x
Reference in New Issue
Block a user