Let's assume that we want to remove 2 upvotes from all the articles of the author with id
Doing this only with Python would execute
N queries (
N being the number of articles in the queryset):
for article in Article.objects.filter(author_id=51): article.upvotes -= 2 article.save() # Note that there is a race condition here but this is not the focus # of this example.
What if instead of pulling all the articles into Python, looping over them, decreasing the upvotes, and saving each updated one back to the database, there was another way?
F() expression, can do it in one query:
Article.objects.filter(author_id=51).update(upvotes=F('upvotes') - 2)
Which can be translated in the following SQL query:
UPDATE app_article SET upvotes = upvotes - 2 WHERE author_id = 51
Why is this better?