Reputation: 12388
I've seen code in the django docs that does multiple object updates like
Entry.objects.filter(pub_date__year=2010).update(comments_on=False)
Is there a way to update multiple objects by updating each object's value? For example, add one to all the articles that a user has read
# so it does something like this?
Entry.objects.filter(user_has_read).update(views+=1)
Upvotes: 7
Views: 8807
Reputation: 62948
Yes, by using F() objects:
from django.db.models import F
Entry.objects.filter(user_has_read).update(views=F('views') + 1)
See updating multiple objects, second to last paragraph.
Upvotes: 14