0

I've been trying to extend the default UserCreationForm to include an email, first and last names. This appears to be quite a common topic on SO however the logic error I am getting I have still not found a solution to.

Here is my code:

forms.py

class RegistrationForm(UserCreationForm):
    first_name = forms.CharField(required=True, label="First name:", max_length=100)
    last_name = forms.CharField(required=True, label="Last name:", max_length=100)
    email = forms.EmailField(required=True)

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

    def save(self, commit=True):
        user = super(RegistrationForm, self).save(commit=False)
        user.email = self.cleaned_data['email']
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        if commit:
            user.save()
        return user

views.py

def register_user(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('/reg_interests/')
    args = {}
    args['form'] = RegistrationForm()
    return render(request, 'login/registration.html', args)

The views and form technically work in the sense that I don't see an error message, however upon user.save() the first and last names are not saved to the database (but the email is).

I know that each of email, first_name and last_name are fields that exist in the default Django User object so the field not being there is not the problem.

How come the extended email will save to the database but the first_name and last_name will not?

Note: I am using the newly released Django 1.10 so I don't know if that has anything to do with it.

Max Goodridge
  • 373
  • 2
  • 7
  • 21
  • what do ```self.cleaned_data['first_name']``` and ```self.cleaned_data['last_name']``` return in RegistationForm.save() after calling super()? – lukeaus Aug 14 '16 at 00:59
  • I think its supposed to grab the data from the corresponding form field in the first part of the class, just like the email – Max Goodridge Aug 14 '16 at 02:00
  • let me restate that... if you add ```print(self.cleaned_data['first_name'])``` and ```print(self.cleaned_data['last_name'])``` above the line ```if commit:``` what do they return on posting the form with data in both last_name and first_name fields? – lukeaus Aug 14 '16 at 03:31
  • I tried that too thinking a `print` statement would at least output a blank line in the console but it doesn't, which makes me think the code isn't being run even though `user.save()` is definitely being run, hence my confusion. – Max Goodridge Aug 14 '16 at 06:47
  • it may not be run if: 1. the form is not valid 2. request.method is not 'POST' 3. The form did not provide required fields for the User model 4. other possibilities... – lukeaus Aug 14 '16 at 17:49
  • I would recommend putting in a debugging statement ```import pdb; pdb.set_trace()``` at the top of register_user and stepping through the code using pdb. – lukeaus Aug 14 '16 at 17:49

0 Answers0