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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With