This file contains hidden or 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
queryset = Entry.objects.all() | |
lst = list(queryset) | |
# returns a list of entry objects | |
first_ten = queryset[:10] | |
# list slicing not queryset slicing because first_ten is a list. | |
first_five = first_ten[:5] | |
This file contains hidden or 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
queryset = Entry.objects.all() | |
# Queries the database because queryset hasn't been evaluated yet. | |
print(queryset[5]) | |
# Queries the database because queryset hasn't been evaluated yet. | |
print(queryset[5]) | |
lst = list(queryset) | |
# Using caches because evaluation happened in previous list(). | |
print(queryset[5]) | |
print(queryset[10]) |
This file contains hidden or 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
entry_list = Entry.objects.all()[1:100:2] | |
This file contains hidden or 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
# repr() evaluates but does not saves results to cache. | |
queryset = Entry.objects.all() | |
str_repr = repr(queryset) | |
# Not using cache.Hitting database again. | |
for each in queryset: | |
print(each.headline) | |
This file contains hidden or 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
# len() evaluates and saves results to cache. | |
queryset = Entry.objects.all() | |
ln = len(queryset) | |
# Using cache from previous evaluation. | |
for each in queryset: | |
print(each.headline) | |
This file contains hidden or 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
# Evaluates the queryset and saves results in cache. | |
queryset = Entry.objects.all() | |
lst = list(queryset) | |
# Using cache from previous list() evaluation. | |
for each in queryset: | |
print(each.headline) |
This file contains hidden or 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
# The `if` statement evaluates the queryset and saves results in cache. | |
queryset = Entry.objects.all() | |
if queryset: | |
# Using cache from previous if statement evaluation. | |
for each in queryset: | |
print(each.headline) | |
This file contains hidden or 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
q1 = Entry.objects.filter(blog=2) | |
q2 = q1.filter(headline__contains='food') | |
entry_list = list(q3) |
This file contains hidden or 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
q = Entry.objects.filter(blog=2).exclude(body_text__icontains="food") | |
q1 = Entry.objects.filter(blog=2) | |
q2 = q1.exclude(body_text__icontains="food") | |
q3 = q2[:10] |