Could not set the value at time of submission using hook

I am trying to update field on submit event ,
i have used following ways to set the value
def generate_pin(PR, method):
code=‘’.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(6))
PR.pin=code

    **OR**

PR.update({"pin":code})

PR stands for Purchase Receipt document

on_submit is called after db is updated.

You have to either update DB yourself or set/update values in before_submit method.