Some of the built-in auth forms only work on user models whose 
`USERNAME_FIELD` is `username`. It is possible to remove this constraint 
and allow them work on any user model. [django-authtools][1] demonstrates 
this possibility.

The two forms in question are `UserCreationForm` and `UserChangeForm`. 
 Both of them explicitly add a `username` field instead of letting the 
ModelForm add it automatically. For `UserChangeForm`, simply removing the 
`username` field from the form achieves user-model independence. 
`UserCreationForm` is slightly more complicated, due to the 
`clean_username` method. `clean_*` methods have to be named after their 
field, so it's hard to add one when you don't know the name of the field. 
This can be overcome by adding a validator to the field while initializing 
the form [2].

The reason the forms do have a `username` field currently is to change the 
help text, validation error messages, and validators. I don't think this 
should happen in the form, because all of these can be set on the model 
field instead. This could cause a backwards-compatibility concern if 
someone wasn't validating usernames in their custom User model (they are 
already validated in `auth.User`), and relied on the form instead. I don't 
think this is a serious issue--it only occurs if someone is using a custom 
User model, using the built-in forms, and not doing any username validation 
in their model.

If this approach sounds reasonable, I will submit it in the form of a patch.

[1]: 
https://github.com/fusionbox/django-authtools/blob/master/authtools/forms.py
[2]: 
https://github.com/fusionbox/django-authtools/blob/master/authtools/forms.py#L61

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to