Reputation: 2965
I tried to track the code in django rest framework.
I was researching the Token how to generate by DRF.
It's DRF repository from github about generating key:
def generate_key(self):
return binascii.hexlify(os.urandom(20)).decode()
I have a doubt. Is it possible to make duplication?
Actually, I don't know why it uses os.urandom
and binascii.hexlify
can generate unique key. Anyone can explain it?
Upvotes: 0
Views: 162
Reputation: 127280
That code doesn't generate unique keys, it generates random (or pseudo-random) keys that are large enough to have a very low probability of being a duplicate.
However, the value is used as the primary key of the model. In most (if not all) databases, primary keys are unique. So if it did happen to generate a duplicate, it would fail when it tried to commit. Although in Django's case it may just assume you were updating the existing record and change the user the token was assigned to. It's not something to reasonably worry about, the probability is too low.
Upvotes: 2