This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for book in Books.objects.all(): | |
do_stuff(book) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for book in Books.objects.all().iterator(): | |
do_stuff(book) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if book.author_id: | |
do_stuff() | |
do_stuff_with_author_id(book.author_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if book.author: | |
do_stuff() | |
# Or | |
do_stuff_with_author_id(book.author.id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> Author.objects.all() | |
SELECT "library_author"."id", "library_author"."name" FROM "library_author" LIMIT 21 | |
Execution time: 0.001393s [Database: default] | |
<QuerySet [<Author: Author object>]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> from django.db import connection | |
>>> connection.queries | |
[] | |
>>> Author.objects.all() | |
<QuerySet [<Author: Author object>]> | |
>>> connection.queries | |
[{u'time': u'0.002', u'sql': u'SELECT "library_author"."id", "library_author"."name" FROM "library_author" LIMIT 21'}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Author(models.Model): | |
name = models.TextField() | |
class Book(models.Model): | |
title = models.TextField() | |
author = models.ForeignKey( | |
Author, on_delete=models.PROTECT, related_name='books', null=True | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> Author.objects.count() | |
20 | |
>>> Book.objects.count() | |
100 | |
# This block is 101 queries. | |
# 1 for the books and 1 for each author that lazy-loaded | |
books = Book.objects.all() | |
for book in books: | |
do_stuff(book.title, book.author.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Retrieve values as a dictionary | |
>>> Book.objects.values('title', 'author__name') | |
<QuerySet [{'author__name': u'Nikolai Gogol', 'title': u'The Overcoat'}, {'author__name': u'Leo Tolstoy', 'title': u'War and Peace'}]> | |
# Retrieve values as a tuple | |
>>> Book.objects.values_list('title', 'author__name') | |
<QuerySet [(u'The Overcoat', u'Nikolai Gogol'), | |
(u'War and Peace', u'Leo Tolstoy')]> | |
>>> Book.objects.values_list('title') | |
<QuerySet [(u'The Overcoat',), (u'War and Peace',)]> |