Marco
Marco

Reputation: 335

How to create users with default password?

I'm creating a form with UserCreationForm to create users but I need that the form just ask for username and email, and assign a default password to each account. So they can change it later.

Upvotes: 0

Views: 1309

Answers (1)

Leandro
Leandro

Reputation: 2247

that?

class UserCreationForm(forms.ModelForm):


    class Meta:
        model = YourUserModel
        fields = ('email', 'username')

    def save(self, commit=True):
        # Save the provided password in hashed format
        user = super(UserCreationForm, self).save(commit=False)

        default_password = somefuntion() #Generate the default password

        user.set_password(default_password ) #Set de default password
        if commit:
            user.save()
        return user

Upvotes: 2

Related Questions