title: Creating a OneToOne Relationship between 2 models(forms) in the same view
body: <p>I am building a signup page.  I am using the default User model and a new model called UserInfo for additional user details.  I need to establish a OneToOne relationship between the 2 models.  To build the signup page, I made a form for each model and put them into the same signup view.</p>
  
  <p>My question is, how to I set user(the OneToOneField) in UserInfo to the User that is being created in the same view UserInfo is created?  If you look in my views.py below, immediately after I saved User's form(signup_form), I tried a few things like..</p>
  
  <ul>
  <li>user2.user = User.objects.get(pk=pk) - doesn't seem to work</li>
  <li>user2.user = request.user - doesn't work because request.user is an anonymous user in this case</li>
  <li>user2.user = user - didn't work</li>
  </ul>
  
  <p><strong>models.py:</strong></p>
  
  <pre><code>class UserInfo(models.Model):
  
      TITLE = (
          ('Salesperson', 'Salesperson'),
          ('Sales Representative', 'Sales Representative'),
          ('Broker', 'Broker'),
          ('Broker of Record', 'Broker of Record'),
      )
  
      user = models.OneToOneField(User, on_delete=models.CASCADE)
      preferred_email = models.EmailField()
      office_phone_number = models.CharField(max_length=10)
      brokerage_of_agent = models.CharField(max_length=50)
      agent_title = models.CharField(max_length=20, choices=TITLE)
  
      def __str__(self):
          return self.preferred_email
  </code></pre>
  
  <p><strong>forms.py:</strong></p>
  
  <pre><code>class SignupForm(UserCreationForm):
      email = forms.EmailField(max_length=200, help_text='Required')
  
      class Meta:
          model = User
          fields = ('username', 'email', 'password1', 'password2')
  
  class UserInfoForm(forms.ModelForm):
  
      class Meta:
          model = UserInfo
          fields = ['preferred_email', 'office_phone_number', 'brokerage_of_agent', 'agent_title']
  </code></pre>
  
  <p><strong>views.py:</strong></p>
  
  <pre><code>def signup(request):
      if request.user.is_authenticated:
          return HttpResponseRedirect('../dashboard/')
      if request.method == 'POST':
          signup_form = SignupForm(request.POST)
          basic_info_form = UserInfoForm(request.POST)
          while (True):  # The while loop is used to get access to 'break' for the @gmail Email check
              if signup_form.is_valid():
                  if not signup_form.cleaned_data['email'].endswith('@gmail.com'):
                      print('You must register with your @gmail.com Email')
                      break
                  user = signup_form.save(commit=False)
                  user.is_active = False
                  user.save()
  
                  ### Look here!  I need to write something here I think, but I'm not sure what to write
                  user2 = basic_info_form.save(commit=False)
                  user2.user = User.objects.get(pk=pk) 
                  user2 = user2.save()
  
                  current_site = get_current_site(request)
                  message = render_to_string('acc_active_email.html', {
                      'user':user, 
                      'domain':current_site.domain,
                      'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                      'token': account_activation_token.make_token(user),
                  })
                  mail_subject = 'Activate your blog account.'
                  to_email = signup_form.cleaned_data.get('email')
                  email = EmailMessage(mail_subject, message, to=[to_email])
                  email.send()
                  return HttpResponse('Please confirm your email address to complete the registration')
      else:
          signup_form = SignupForm()
          basic_info_form = UserInfoForm()
  
      return render(request, 'signup.html', {'signup_form': signup_form, 'basic_info_form': basic_info_form})
  </code></pre>
  
  <p><strong>HTML Template:</strong></p>
  
  <pre><code>{% extends "base.html" %}
  {% load static %}
  
  {% block content %}
  &lt;h2&gt;Sign up&lt;/h2&gt;
  &lt;form method="post"&gt;
      {% csrf_token %}
      {% for field in signup_form %}
      &lt;p&gt;
          {{ field.label_tag }}&lt;br&gt;
          {{ field }}
          {% if field.help_text %}
          &lt;small style="display: none"&gt;{{ field.help_text }}&lt;/small&gt;
          {% endif %}
          {% for error in field.errors %}
      &lt;p style="color: red"&gt;{{ error }}&lt;/p&gt;
          {% endfor %}
      {% endfor %}
  
      {% for field in basic_info_form %}
      &lt;p&gt;
          {{ field.label_tag }}&lt;br&gt;
          {{ field }}
          {% if field.help_text %}
          &lt;small style="display: none"&gt;{{ field.help_text }}&lt;/small&gt;
          {% endif %}
          {% for error in field.errors %}
      &lt;p style="color: red"&gt;{{ error }}&lt;/p&gt;
          {% endfor %}
      {% endfor %}
  
  
      &lt;button type="submit"&gt;Sign up&lt;/button&gt;
  &lt;/form&gt;
  {% endblock %}
  </code></pre>