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
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 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
# Don't | |
for entry in Entry.objects.all(): | |
entry.likes += 1 | |
entry.save() | |
# Do | |
Entry.objects.update(likes=F('likes') + 1) |
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
# 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 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
# 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 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
# 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 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
# 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 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
class Author(models.Model): | |
name = models.CharField(max_length=200) | |
email = models.EmailField() | |
def __str__(self): | |
return self.name | |
class Blog(models.Model): | |
name = models.CharField(max_length=100) | |
tagline = models.TextField() |
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
# Don't. Needs database hit | |
blog_id = Entry.objects.get(id=200).blog.id | |
# Do. The foreign key is already cached, so no database hit | |
blog_id = Entry.objects.get(id=200).blog_id | |
# Do. No database hit | |
blog_id = Entry.objects.select_related('blog').get(id=200).blog.id |
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
Reference: | |
Django/Postgres/Nginx/Gunicorn in Ubuntu: https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04 | |
Encryption: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04 | |
Where you see "user", "myproject" and "myprojectuser" replace that with what is relevent to what you are working on. | |
User Setup | |
$ ssh root@SERVER_IP_ADDRESS | |
Complete login process |