I am trying to change order of nodes in my tree. Everything works fine, but I would like to know if there is some beautiful, easy way of updating multiple fields by increasing its actual value by 1. Let me illustrate.
Objtree.objects.select_related().filter(pk__in = ids).update(sort_order = 1)
This code will change every sort_order column value to 1, but I would like to change it to something like:
Objtree.objects.select_related().filter(pk__in = ids).update(sort_order += 1) # or Objtree.objects.select_related().filter(pk__in = ids).update(self.sort_order = 1)
So... is there something like that? Nothing comes to my mind or my screen via googling.
Thanks for halp!
You want to use F()
objects.
from django.db.models import F Objtree.objects.filter(pk__in=ids).update(sort_order=F('sort_order')+1)
See the documentation
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