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 8372751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:30:58+00:00 2026-06-09T14:30:58+00:00

I have a User Model (pre-defined by Django) and a UserProfile model connected via

  • 0

I have a User Model (pre-defined by Django) and a UserProfile model connected via a ForeignKey.

I’m creating two separate ModelForms to use in a single template as a registration form of sorts.

models.py

class UserProfile(models.Model):
    # This field is required.
    user = models.ForeignKey(User, unique=True, related_name="connector")
    location = models.CharField(max_length=20, blank=True, null=True)

forms.py

class UserForm(ModelForm):
    class Meta:
        model = User

class UserProfileForm(ModelForm):
    class Meta:
        model = UserProfile

However, when I load the page and fill in the form information, the UserProfileForm requires me to select / fill in the user field in order to validate.

This “user”, however, is the one that is being created right now so I can’t choose a “user” as it has not been created yet / is still being created.

I know this user is field has the attribute unique=true but there a way to make this field “optional”?

My view (below) should handle it such that once the user object is created, i will set the foreign key of UserProfile to this newly created user object (unless I am doing something wrong my views.py as well).

views.py

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

            #create entry for UserProfile (extension of new_user object)      
            profile = form2.save(commit = False)
            profile.user = new_user
            profile.save()
            return HttpResponseRedirect("/books/")
    else:
        form1 = UserForm()
        form2 = UserProfileForm()
    c = {
        'form1':form1,
        '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-09T14:30:59+00:00Added an answer on June 9, 2026 at 2:30 pm

    Well you could set:

    user = models.ForeignKey(User, unique=True, blank=True, null=True, related_name="connector")
    

    which would make the user relationship optional, but I don’t think this is what you need. Instead, why not just remove the user field from the UserProfile form and manually assign it, instead of letting the user see the dropdown at all. Then there would be no validation error.

    class UserProfileForm(ModelForm):
        class Meta:
            model = UserProfile
            exclude = ("user", )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models: User (pre-defined by Django) and UserProfile that are connected through
I have two models in Django: User (pre-defined by Django) and UserProfile. The two
I have a User model and a Submission model. Each Submission has a ForeignKey
I have a User model, and use an acts_as_authentic (from authlogic) on it. My
I have 2 models class Vhost(models.Model): dns = models.ForeignKey(DNS) user = models.ForeignKey(User) extra =
Let's say I have User model and Post model. Post model contains field user_id
I have a User model that has many posts. Im trying to render the
I have a user model on which I check to make sure that the
I have a user model that requires the user to change their password every
I have a User model, a Post model, and an Interest model. User has_many

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.