Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django queryset update() in += way

Tags:

python

django

How can I call update on Django queryset, that does not write particular value, but modify the value already there, as follows

Entry.objects.filter(pub_date__year=2010).update(count+=1)

How can I achieve that?

like image 306
matousc Avatar asked Dec 07 '22 19:12

matousc


1 Answers

You can use F expression from docs:

reporter = Reporters.objects.filter(name='Tintin')

reporter.update(stories_filed=F('stories_filed') + 1)

Try this:

from django.db.models import F
Entry.objects.filter(pub_date__year=2010).update(count=F('count') + 1)
like image 192
neverwalkaloner Avatar answered Jan 10 '23 21:01

neverwalkaloner