I’m trying to provide a user registration form without requiring a username to be manually entered. I setup an authentication backend so that users can authenticate with an email address and that works because I can login with the Django admin user. The idea is that my app would create a SHA1 hash of the email address to be stored as the Django username and the user would never see this.
I removed the username field from my registration html template, but I’m not sure how or where I should be generating the username programmatically. I think it should be in the clean_username method of the RegistrationForm, but that method doesn’t get called when I use a template without the username field.
Any help would certainly be appreciated.
I got it to work. I don’t feel good about having to modify the registration’s view method and the default registration backend directly. I’d prefer to have those changes in my own code and am still working to make those changes, but this does indeed work.
Here’s how I did it:
Created a custom registration backend called RegBackend that generates a sha1 hash based on the email address and then stores the hexdigest as the username, finally returning a User object.
Map register in urls.py to the new RegBackend that I created in step 1
Modified the django-registration’s view register method to create a random username so that the form validates, but I never persist the random username. I copied the request.POST dictionary and set this random username to one of the copy’s dictionary keys called data[‘username’]. Then I use the data variable when creating an instance of the form_class. Calling is_valid() on the form would return false because I removed the username from the template, but Django requires a username for registration, so I needed to supply it something.
I didn’t use the sha1 hash for the random username because the Django username can only be 30 characters in length while the sha1 hash is 40. Oddly, the custom registration backend didn’t complain and can store the sha1 hash, but the form would generate errors because of the length when submitted.
_init_.py (I copied the existing DefaultBackend and modified with the SHA1 portion)
urls.py
registration/views.py