3

I found this post on how to extend the UserCreationForm with extra fields such as "email." However, the email field is already defined in the pre-built user model.

I created an extra model (called UserProfile) that futher extends Django's pre-built User class. How do I get these fields I defined in UserProfile to appear in my UserCreationForm?

goelv
  • 2,774
  • 11
  • 32
  • 40

1 Answers1

14

Add fields as appropriate for your UserProfile model (it's not too easy to use a ModelForm to avoid Repeating Yourself, unfortunately), then create and save a new UserProfile instance in the over-ridden save() function. Adapted from the post you linked to:

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


class UserCreateForm(UserCreationForm):
    job_title = forms.CharField(max_length=100, required=True)
    age = forms.IntegerField(required=True)

    class Meta:
        model = User

    def save(self, commit=True):
        if not commit:
            raise NotImplementedError("Can't create User and UserProfile without database save")
        user = super(UserCreateForm, self).save(commit=True)
        user_profile = UserProfile(user=user, job_title=self.cleaned_data['job_title'], 
            age=self.cleaned_data['age'])
        user_profile.save()
        return user, user_profile
supervacuo
  • 9,072
  • 2
  • 44
  • 61
  • thanks! just to clarify for myself, job_title and age are fields that are defined in UserProfile right? We know that UserProfile is connected to User via a foreign key. Does this mean if I create a Model Form for a model like User, the fields from any of User's foreign key can also be used in the Form then? – goelv Aug 11 '12 at 23:27
  • 1
    `job_title` and `age` were, yes, imaginary fields on your `UserProfile` model. The key is that you can add whatever fields you like to a `ModelForm`; ones not related to the model specified in `Meta` are ignored, and it's up to you to process them. That's what my suggested code is doing (pulling the values from `self.cleaned_data`. – supervacuo Aug 11 '12 at 23:29
  • 1
    *i.e.* adding the fields would still work even if those weren't model fields on `UserProfile`, you just wouldn't be able to do anything meaningful with them in `save()`. There's nothing special about `UserProfile` here. – supervacuo Aug 11 '12 at 23:31
  • OH, I see! Last question: how do you call the form's save method / what does it do? What I'm getting at is that when I'm processing the form in my views.py, how can I access the fields that the user has just inputted data for? – goelv Aug 11 '12 at 23:35
  • You only need something like `form = UserCreateForm(request.POST)` followed by `if form.is_valid(): user, user_profile = form.save()`. See the "[Working with forms](https://docs.djangoproject.com/en/1.4/topics/forms/#using-a-form-in-a-view)" and "[Creating forms from models](https://docs.djangoproject.com/en/1.4/topics/forms/modelforms/)" docs for more. – supervacuo Aug 11 '12 at 23:45