Art
Art

Reputation: 24597

Django: use hash/guid in username

I am going to use email as a username across the website, however I still need to pre-fill the mandatory username field in User model somehow.

Initially I was thinking of using a md5 hash of the email as username, but given the limitation of 30 characters it is not possible. Also I don't think I can use GUIDs for that as they are also longer than 30 chars when converted to string hex.

Any suggestions greatly appreciated!

Upvotes: 1

Views: 890

Answers (2)

boatcoder
boatcoder

Reputation: 18107

These links string length of a GUID, and Characters in a GUID show that a guid is really only 16 characters long. Its the ASCII equivalent that is longer. So, as long as you convert back and forth before display (or if you aren't going to display them at all), a GUID fits in the username field nicely.

Upvotes: 1

Dominic Rodger
Dominic Rodger

Reputation: 99811

I wouldn't stress too much about GUIDs being longer than 30 characters. A reasonable approach is probably to hash the GUID using something like MD5, and then trim off the last 2 characters. Your chances of a collision are effectively nil. (1630 is an awfully large number).

Upvotes: 3

Related Questions