[Django]-Excluding password when extending UserCreationForm

2👍

It’s not a bad idea to have a look at a class you subclass. password1 and password2 fields are defined in form directly, not in the model. So exclude and fields will have no effect on them. Just make your own ModelForm as @MatthewSchinckel suggests.

5👍

You can just remove the password2 field in the init of the form like so:

class MyUserCreationForm(UserCreationForm):

    def __init__(self, *args, **kargs):
        super(MyUserCreationForm, self).__init__(*args, **kargs)
        del self.fields['password2']

1👍

Why not just use a ModelForm, and exclude the fields you don’t want? That seems like it would be a simpler solution.

1👍

think that would give the idea…

class RegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True)
    #first_name = forms.CharField(required=True)
    #last_name = forms.CharField(required=True)
    username = forms.CharField (required=True)

class Meta:
    model = User
    fields = ('first_name','last_name','username','email', 'password1', 'password2')

def __init__ (self, *args, **kwargs):
    super(RegistrationForm,self).__init__(*args, **kwargs)
    #remove what you like...
    self.fields.pop ('first_name')
    self.fields.pop ('last_name')
    self.fields.pop ('password1')
    self.fields.pop ('password2')
👤Sarp

Leave a comment