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 limitatio开发者_StackOverflown 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!
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).
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.
精彩评论