import time def gen_next_id(id_before): str_list = [] num_list = [] before = 0 for i in range(len(id_before)-1): if id_before[i].isnumeric() + id_before[i + 1].isnumeric() == 1: if id_before[i].isnumeric(): num_list.append(id_before[before:i+1]) before = i+1 else: str_list.append(id_before[before:i+1]) before = i+1 if id_before[before].isnumeric(): num_list.append(id_before[before:len(id_before)]) else: str_list.append(id_before[before:len(id_before)]) num_list[-1] = str(int(num_list[-1]) + 1) id_after = '' for str_item, num_item in zip(str_list, num_list): id_after = id_after + str_item + num_item return id_after def get_id(model_manager, first_id): if not model_manager.objects.last(): return first_id else: time_start = time.time() next_id = gen_next_id(model_manager.objects.order_by('create_time').last().id) return next_id if __name__ == '__main__': id_a = gen_next_id("TestLaboratory_V1_Version_1") # a = gen_next_id("a100a_V1_11") # print(a) # print('2'.isnumeric() + '1'.isnumeric())