matousc matousc - 4 years ago 149
Python Question

Django queryset update() in += way

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?

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download