Create User ModelForm and working with it

I’m not sure why you need to create your own form since UserCreationForm exists, but, if you must create the form yourself, then I think you’d need roughly:

  • A clean_username method to ensure the username is not already associated with another user
  • A clean_email method for a similar purpose (and I might suggest that you use EmailField on your model to save you a lot of extra validation work).
  • A clean method that can compare the two passwords and make sure they match. It would also need to call validate_password to run the password through all the validators and make sure it’s not too weak.
  • A save method that creates a User object. As you’ve written your UserModel at the moment, you’d be storing the password in plain text. That’s not good. I’d suggest you consider reading https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#specifying-custom-user-model to learn how to create a custom user model that does password management correctly. That would give you access to user.set_password so that you can store your password as a hash. Any of your future users will thank you. :smile:

I hope that helps a bit.

1 Like