生成随机不重复ID
def random_string_generator(size=10, chars=string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _ in range(size))
def unique_order_id_generator(instance):
"""
This is for a Django project with an order_id field
"""
order_new_id = random_string_generator()
Klass = instance.__class__
qs_exists = Klass.objects.filter(order_id=order_new_id).exists()
if qs_exists:
return unique_order_id_generator(instance)
return order_new_id