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
# Don't | |
for i in range(20): | |
Blog.objects.create(name="blog"+str(i), headline='tagline'+str(i)) | |
# Do | |
blogs = [] | |
for i in range(20): | |
blogs.append(Blog(name="blog"+str(i), headline='tagline'+str(i))) | |
Blog.objects.bulk_create(blogs) |
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
# Don't.Delete one by one. | |
for entry in Entry.objects.all(): | |
entry.delete() | |
# Do.Delete all at once. | |
Entry.objects.all().delete() | |
# Don't | |
for entry in Entry.objects.all(): | |
entry.likes += 1 |
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
# Don't | |
count = len(Entry.objects.all()) # Evaluates the entire queryset | |
# Do | |
count = Entry.objects.count() # Executes more efficient SQL to determine count | |
# Don't | |
qs = Entry.objects.all() | |
if qs: |
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
# Don't | |
most_liked = 0 | |
for entry in Entry.objects.all(): | |
if entry.likes > most_liked: | |
most_liked = entry.likes | |
# Do | |
most_liked = Entry.objects.all().aggregate(Max('likes'))['likes__max'] |
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
# Don't | |
for entry in Entry.objects.all(): | |
entry.likes += 1 | |
entry.save() | |
# Do | |
Entry.objects.update(likes=F('likes') + 1) |
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
try: | |
one_entry = Entry.objects.get(blog=2000) | |
except Entry.DoesNotExist: | |
# query did not match to any item. | |
pass | |
except Entry.MultipleObjectsReturned: | |
# query matched multiple items. | |
pass | |
else: |
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
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] |
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
q1 = Entry.objects.filter(blog=2) | |
q2 = q1.filter(headline__contains='food') | |
entry_list = list(q3) |