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

  • SEARCH
  • Home
  • 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 8375299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:08:23+00:00 2026-06-09T15:08:23+00:00

I have two models in Django: User (pre-defined by Django) and UserProfile. The two

  • 0

I have two models in Django: User (pre-defined by Django) and UserProfile. The two are connected via a Foreign Key.

models.py:

class UserProfile(models.Model):
  user = models.ForeignKey(User, unique=True, related_name="connect")
  location = models.CharField(max_length=20, blank=True, null=True)

I’m using UserCreationForm (pre-defined by Django) for the User Model, and created another form for UserProfile in forms.py

#UserCreationForm for User Model

class UserProfileForm(ModelForm):
  class Meta:
    model = UserProfile
    exclude = ("user", )

I load both these forms in a template, registration.html, so the website customer can enter data about fields contained in both models (ex: “first_name”, “last_name” in User model, “location” in UserProfile model).

For the life of me, I can’t figure out how to create a view for this registration form. What I’ve tried so far will create the User object, but it will not associate other information such as location in the corresponding UserProfile object. Can anyone help me out? This is what I currently have:

def register(request):
  if request.method == 'POST':
    form1 = UserCreationForm(request.POST)
    form2 = UserProfileForm(request.POST)
    if form1.is_valid():
      #create initial entry for User object
      username = form1.cleaned_data["username"]
      password = form1.cleaned_data["password"]
      new_user = User.objects.create_user(username, password)

      # What to do here to save "location" field in a UserProfile 
      # object that corresponds with the new_user User object that 
      # we just created in the previous lines

  else:
    form1 = UserCreationForm()
    form2 = UserProfileForm()
  c = {
    'form1':UserCreationForm,
    'form2':form2,
  }
  c.update(csrf(request))
  return render_to_response("registration/register.html", c)
  • 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-06-09T15:08:24+00:00Added an answer on June 9, 2026 at 3:08 pm

    Almost there 🙂

    def register(request):
        if request.method == 'POST':
            form1 = UserCreationForm(request.POST)
            form2 = UserProfileForm(request.POST)
            if form1.is_valid() and form2.is_valid():
                user = form1.save()  # save user to db
                userprofile = form2.save(commit=False)  # create profile but don't save to db
                userprofile.user = user
                userprofile.location = get_the_location_somehow()
                userprofile.save()  # save profile to db
    
        else:
            form1 = UserCreationForm()
             form2 = UserProfileForm()
        c = {
          'form1':form1,
          'form2':form2,
        }
        c.update(csrf(request))
        return render_to_response("registration/register.html", c)
    

    To clarify a bit, form.save() creates an instance of the Model and saves it to db. form.save(commit=False) Just creates an instance, but is does not save anything to db.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a User Model (pre-defined by Django) and a UserProfile model connected via
I have two models: User (pre-defined by Django) and UserProfile that are connected through
I have the following two models: class Position(models.Model): position = models.CharField(max_length=100) class UserProfile(models.Model): user
I have two models, Picture and SubmittedPicture as follows: class Picture(models.Model): user = models.ForeignKey(User)
I have two models in my Django 1.1.1 application: class UserRequest(models.Model): # blah blah
In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
Say I have two models in my Django project. class Project(models.Model): name = models.CharField(max_length=256)
I have two models, one a vanilla Django model, another an unmanaged model wrapping
I have two models, Order and UserProfile. Each Order has a ForeignKey to UserProfile,
DUPLICATE: Using a Django custom model method property in order_by() I have two models;

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.