nodes: 0, module; 1, function_definition; 2, function_name:_update; 3, parameters; 4, block; 5, identifier:qs; 6, expression_statement; 7, try_statement; 8, comment:"""
          Increment the sort_order in a queryset.
  
          Handle IntegrityErrors caused by unique constraints.
          """; 9, block; 10, except_clause; 11, with_statement; 12, identifier:IntegrityError; 13, block; 14, with_clause; 15, block; 16, for_statement; 17, with_item; 18, expression_statement; 19, identifier:obj; 20, call; 21, block; 22, call; 23, call; 24, attribute; 25, argument_list; 26, expression_statement; 27, attribute; 28, argument_list; 29, attribute; 30, argument_list; 31, identifier:qs; 32, identifier:order_by; 33, string; 34, call; 35, identifier:transaction; 36, identifier:atomic; 37, identifier:qs; 38, identifier:update; 39, keyword_argument; 40, string_content:-sort_order; 41, attribute; 42, argument_list; 43, identifier:sort_order; 44, binary_operator:models.F('sort_order') + 1; 45, call; 46, identifier:update; 47, keyword_argument; 48, call; 49, integer:1; 50, attribute; 51, argument_list; 52, identifier:sort_order; 53, binary_operator:models.F('sort_order') + 1; 54, attribute; 55, argument_list; 56, identifier:qs; 57, identifier:filter; 58, keyword_argument; 59, call; 60, integer:1; 61, identifier:models; 62, identifier:F; 63, string; 64, identifier:pk; 65, attribute; 66, attribute; 67, argument_list; 68, string_content:sort_order; 69, identifier:obj; 70, identifier:pk; 71, identifier:models; 72, identifier:F; 73, string; 74, string_content:sort_order
code: def _update(qs):
          """
          Increment the sort_order in a queryset.
  
          Handle IntegrityErrors caused by unique constraints.
          """
          try:
              with transaction.atomic():
                  qs.update(sort_order=models.F('sort_order') + 1)
          except IntegrityError:
              for obj in qs.order_by('-sort_order'):
                  qs.filter(pk=obj.pk).update(sort_order=models.F('sort_order') + 1)