[Answered ]-Why isn't the number gotten from this random code generator function not being saved on the DB?

1👍

You don’t return the recursive call:

def GetRandomRedeemCode():
    random_num = str(random.randint(10000000, 99999999))
    if Purchase.objects.filter(redeem_code=random_num).exists():
        return GetRandomRedeemCode()  # 🖘 return the result
    else:
        return random_num

You might however better use a while loop here:

def GetRandomRedeemCode():
    random_num = str(random.randint(10000000, 99999999))
    while Purchase.objects.filter(redeem_code=random_num).exists():
        random_num = str(random.randint(10000000, 99999999))
    return random_num

Leave a comment