Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django create custom UserCreationForm

Tags:

I enabled the user auth module in Django, however when I use UserCreationForm it only asks for username and the two password/password confirmation fields. I also want email and fullname fields, all set as required fields.

I've done this:

from django.contrib.auth.forms import UserCreationForm from django import forms from django.contrib.auth.models import User  class RegisterForm(UserCreationForm):     email = forms.EmailField(label = "Email")     fullname = forms.CharField(label = "Full name")      class Meta:         model = User         fields = ("username", "fullname", "email", ) 

Now the form shows the new fields but it doesn't save them to the database.

How can I fix this?

like image 215
Joaquin McCoy Avatar asked Apr 21 '11 14:04

Joaquin McCoy


People also ask

What is django UserCreationForm?

Django UserCreationForm is used for creating a new user that can use our web application. It has three fields: username, password1, and password2(which is basically used for password confirmation). To use the UserCreationForm, we need to import it from django.

What is form Is_valid () in django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.


2 Answers

You have to override UserCreationForm.save() method:

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

http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/forms.py#L10

like image 37
manji Avatar answered Sep 19 '22 17:09

manji


There is no such field called fullname in the User model.

If you wish to store the name using the original model then you have to store it separately as a first name and last name.

Edit: If you want just one field in the form and still use the original User model use the following:

You can do something like this:

from django.contrib.auth.forms import UserCreationForm from django import forms from django.contrib.auth.models import User  class RegisterForm(UserCreationForm):     email = forms.EmailField(label = "Email")     fullname = forms.CharField(label = "First name")      class Meta:         model = User         fields = ("username", "fullname", "email", ) 

Now you have to do what manji has said and override the save method, however since the User model does not have a fullname field it should look like this:

def save(self, commit=True):         user = super(RegisterForm, self).save(commit=False)         first_name, last_name = self.cleaned_data["fullname"].split()         user.first_name = first_name         user.last_name = last_name         user.email = self.cleaned_data["email"]         if commit:             user.save()         return user 

Note: You should add a clean method for the fullname field that will ensure that the fullname entered contains only two parts, the first name and last name, and that it has otherwise valid characters.

Reference Source Code for the User Model:

http://code.djangoproject.com/browser/django/trunk/django/contrib/auth/models.py#L201 
like image 86
chandsie Avatar answered Sep 21 '22 17:09

chandsie