Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6628639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:08:53+00:00 2026-05-25T22:08:53+00:00

I am using extended version of the UserCreationForm to add users via my own

  • 0

I am using extended version of the UserCreationForm to add users via my own template, which is working well.

I would also like to include, as part of the same form template, a custom field from my userprofile model, so that when the user is created a user profile with my custom field would also be created.

My approach to this has been to use two forms and combine them in one template with a single submit button.

The form displays exactly as I wanted, and returns validation errors correctly, but predictably it falls down when it comes to saving to the database. When I call save() on the user form the user is created, but of course when I try to save the userprofile form it throws an error because the user doesn’t yet exist so it has not user associated to it.

Although I think I understand the cause of my problem, I am at a loss as to how to fix it, I am not even sure if the approach that I have taken is correct.

I have included all my code below including the model as well as the forms and the view just in case this helps anyone to better understand what I am trying to do:

UserProfile class (models.py)

LEVEL = (
    ('admin', 'administrator'),
    ('team', 'team leader'),
    ('member', 'team member'),
)

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    level = models.CharField(choices=LEVEL, max_length=20)

UserCreationForm and UserProfileForm classes (forms.py)

class UserCreationFormExtended(UserCreationForm): 
    def __init__(self, *args, **kwargs): 
        super(UserCreationFormExtended, self).__init__(*args, **kwargs) 
        self.fields['first_name'].required = True
        self.fields['last_name'].required = True

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


class UserProfileForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(UserProfileForm, self).__init__(*args, **kwargs)  

         self.fields["level"].choices = ( ('admin', 'administrator'), ('team', 'team leader'), ('member', 'team member') )

    class Meta:
        model = UserProfile

use_add view (views.py)

def user_add(request):

    if request.method == 'POST':
        uform = UserCreationFormExtended(request.POST)
        pform = UserProfileForm(request.POST)

        if uform.is_valid():

            uform.save()
            pform.save()

            return render_to_response('user/add_success.html', context_instance=RequestContext(request))

        else:
            return render_to_response('user/add.html', { 'uform' : uform, 'pform' : pform }, context_instance=RequestContext(request))

    else:
        uform = UserCreationFormExtended()
        pform = UserProfileForm()

        return render_to_response('user/add.html', { 'uform' : uform, 'pform' : pform }, context_instance=RequestContext(request))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:08:54+00:00Added an answer on May 25, 2026 at 10:08 pm

    First, add exclude = ('user',) to the Meta class for ProfileForm. Then, in your view:

    user_valid = uform.is_valid()
    profile_valid = pform.is_valid()
    if user_valid and profile_valid:
        user = uform.save()
        profile = pform.save(commit=False)
        profile.user = user
        profile.save()
    

    Although it occurs to me that since you only have one field on the profile form, an easier way to do it is to forget that form completely, and just add the field to the user form:

    class UserCreationFormExtended(UserCreationForm): 
        level = forms.ChoiceField(choices=LEVEL, max_length=20)
        ... etc...
    
    if uform.is_valid():
        user = uform.save()
        profile = Profile.objects.create(user=user, level=uform.cleaned_data['level']))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I extended the functionality of a line object using a class which takes a
I'm using the Prototype.js (from www.prototypejs.org) library to create classes which are extended by
I'm using an extended version of BaseAdapter based on the EfficientAdapter example from the
I need to write a extended version of the StringUtils.commaDelimitedListToStringArray function which gets an
I have a treeview (using the dyna tree version) and I have extended the
I'm using the 'Extended WPF Toolkit' ( http://wpftoolkit.codeplex.com/ ), and for my own purposes
This is actually extended version of Alternatives to using an activity for each tab
I am using (a slightly extended version of) the following code in a factory-pattern
I am working on a project that is built on an extended version of
How can I develop applications in C# that can be further extended using DLL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.