Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #26381 -- Made UserCreationForm reusable with custom user models that define USERNAME_FIELD. #6315

Closed
wants to merge 1 commit into from
Closed

Fixed #26381 -- Made UserCreationForm reusable with custom user models that define USERNAME_FIELD. #6315

wants to merge 1 commit into from

Conversation

berkerpeksag
Copy link
Contributor

@@ -82,7 +82,9 @@ class Meta:

def __init__(self, *args, **kwargs):
super(UserCreationForm, self).__init__(*args, **kwargs)
self.fields['username'].widget.attrs.update({'autofocus': ''})
username_field = self.fields.get('username')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

self.fields[self._meta.model.USERNAME_FIELD]? I think it's a mandatory field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking something similar to https://github.com/django/django/blob/master/django/contrib/auth/forms.py#L158, but your suggestion is better. Done, thanks!

I also removed the if branch. Perhaps we could catch the KeyError and raise an exception with a more informed message (e.g. USERNAME_FIELD cannot be found in FooCreationForm.Meta.fields)?

@charettes
Copy link
Member

As USERNAME_FIELD is documented as mandatory I'm not sure it's worth raising a more descriptive exception.

@timgraham
Copy link
Member

merged in efa9539, thanks!

@timgraham timgraham closed this Mar 21, 2016
@berkerpeksag berkerpeksag deleted the 26381-usercreationform-custom-username-field branch March 21, 2016 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants