util_add_id.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import time
  2. def gen_next_id(id_before):
  3. str_list = []
  4. num_list = []
  5. before = 0
  6. for i in range(len(id_before)-1):
  7. if id_before[i].isnumeric() + id_before[i + 1].isnumeric() == 1:
  8. if id_before[i].isnumeric():
  9. num_list.append(id_before[before:i+1])
  10. before = i+1
  11. else:
  12. str_list.append(id_before[before:i+1])
  13. before = i+1
  14. if id_before[before].isnumeric():
  15. num_list.append(id_before[before:len(id_before)])
  16. else:
  17. str_list.append(id_before[before:len(id_before)])
  18. num_list[-1] = str(int(num_list[-1]) + 1)
  19. id_after = ''
  20. for str_item, num_item in zip(str_list, num_list):
  21. id_after = id_after + str_item + num_item
  22. return id_after
  23. def get_id(model_manager, first_id):
  24. if not model_manager.objects.last():
  25. return first_id
  26. else:
  27. time_start = time.time()
  28. next_id = gen_next_id(model_manager.objects.order_by('create_time').last().id)
  29. return next_id
  30. if __name__ == '__main__':
  31. id_a = gen_next_id("TestLaboratory_V1_Version_1")
  32. # a = gen_next_id("a100a_V1_11")
  33. # print(a)
  34. # print('2'.isnumeric() + '1'.isnumeric())