Quick example of extending UserCreationForm in Django

by jesse in ,

I just banged my head against this, and with no good answers floating around out there, I thought I'd share. In my case, I just wanted to extend the basic django.contrib.auth.forms.UserCreationForm in order to make it so when a user was added, an email address had to be supplied in addition to the username and password fields.

Here is a working example (forms.py) - just so I don't forget it:

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm

class UserCreateForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = User
        fields = ("username", "email", "password1", "password2")

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

You have to modify the save method on the form to add the email to user object returned by the super call. You can use this to expose other fields on the User object as needed.