title: Testing a model with a one-to-one relationship
body: <p>Im testing my app and am really stuck on how to test my personalInformation form which has a one-to-one relationship with my user. I have tried a lot of things but here is the testing code boiled down to the stuff im pretty sure is correct and a few related classes.</p>
  
  <p>models.py</p>
  
  <pre><code>class PersonalInformation(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE)
  
      GENDERS = (
          ('M', 'Male'),
          ('F', 'Female'),
      )
  
      first_name = models.CharField(max_length=200, default='')
      surname = models.CharField(max_length=200, default='')
      gender = models.CharField(max_length=1, choices=GENDERS)
      dob = models.DateTimeField('Date of birth (mm/dd/yyyy)', null=True, default=now)
  
      def __str__(self):
          return f'{self.user.username}'
  </code></pre>
  
  <p>forms.py</p>
  
  <pre><code>class PersonalInformationForm(forms.ModelForm):
  
      def __init__(self, *args, **kwargs):
          super(PersonalInformationForm, self).__init__(*args, **kwargs)
          self.helper = FormHelper()
          self.helper.form_method = 'post'
          self.helper.add_input(Submit('submit', 'Submit'))
  
      class Meta:
          model = PersonalInformation
  
          fields = ['first_name', 'surname', 'gender', 'dob']
  
          widgets = {
              'dob': DatePickerInput(
                  options={
                      "format": "MM/DD/YYYY",
                      "showClose": False,
                      "showClear": False,
                      "showTodayButton": False,
                  }
              ),
          }
  </code></pre>
  
  <p>unit test</p>
  
  <pre><code>def test_user_information_updated_on_success(self):
          user = User.objects.create_superuser('username')
          self.client.force_login(user)
          form = PersonalInformationForm(data={
              'first_name': 'testuser',
              'surname': 'testsurname',
              'gender': 'M',
              'dob': '1984-09-17 00:00:00'})
          form.save() # form.save throws error
  </code></pre>
  
  <p>error msg</p>
  
  <pre><code>1048, "Column 'user_id' cannot be null"
  </code></pre>
  
  <p>This isnt correct as 'first_name' is an attribute of personalInformation, which has a one-to-one relationship with the user</p>
  
  <pre><code>self.assertEqual(user.first_name, 'testuser')
  </code></pre>
  
  <p>-- EDIT --</p>
  
  <p>Following Arakkal Abu's advice I now get</p>
  
  <pre><code>Duplicate entry '12' for key 'user_id'
  </code></pre>
  
  <p>I think maybe I should be testing this by calling</p>
  
  <pre><code>self.client.post(
      '/enrolment/personal_information/', data={
      'first_name': 'testuser',
      'surname': 'testsurname',
      'gender': 'M',
      'dob': '1984-09-17 00:00:00'}
  )
  </code></pre>
  
  <p>so im calling the POST method on the page im testing, but it doesnt update the attributes as when i call</p>
  
  <pre><code>assertEqual(user.personalinformation.first_name, 'testuser')
  </code></pre>
  
  <p>it is blank.</p>
  
  <p>Thank you very much.</p>