[Fixed]-How to properly overwrite save method in custom User Profile Creation Form?

1👍

First, you don’t need to redifine some fields like username, email, etc…
Here is a complete example with a save ovride:

class RegisterForm(UserCreationForm):
    email = forms.EmailField()

    def __init__(self, *args, **kwargs):
        super(RegisterForm, self).__init__(*args, **kwargs)
        #self.error_class = BsErrorList

    def clean_email(self):
        email = self.cleaned_data["email"]

        try:
            User._default_manager.get(email__iexact=email)
        except User.DoesNotExist:
            return email
        raise forms.ValidationError(
            'A user with that email already exists',
            code='duplicate_email',
        )

    def save(self, commit=True):
        user = super(RegisterForm, self).save(commit=False)
        user.email = self.cleaned_data["email"]

        # Create user_profile here

        if commit:
            user.save()

        return user

Edit: This solution reuse django’s User form to register a new User then you have to create your custom profile.

👤ahmed

Leave a comment