title: Django Foreign Key many to one not working
body: <p>I am abit confused on how to make a foreing key work with many to one relationship.</p>
  <p>I have 2 Models, auditsmodel (Which is the model containing the data) and dv_mode(Containing data validation as options for the answers).</p>
  <p>I have created a Foreign key in auditsmodel to reference the Data validation options.</p>
  <p>Models.py</p>
  <pre><code>class dv_model(models.Model):
      # auditsModelKey = models.ForeignKey(auditsModel, on_delete = models.CASCADE)
      Defect_Area_dv = models.CharField(_('Defect Area dv'), max_length=200)
      Key_Driver_dv = models.CharField(_('Key Driver dv'), max_length=200)
      Sub_Key_Driver_dv = models.CharField(_('Sub Key Driver dv'), max_length=200)
      QS_Login_dv = models.CharField(_('QS Login dv'), max_length=200)
      QS_Name_dv = models.CharField(_('QS Name dv'), max_length=200)
      Status_dv = models.CharField(_('Status dv'), max_length=200)
      Metric_name_dv = models.CharField(_('Metric name dv'), max_length=200, default='')
      Correct_Associate_Action_dv = models.CharField(_('Correct Associate Action dv'), max_length=200)
      Correct_investigator_Action_dv = models.CharField(_('Correct investigator Action dv'), max_length=200)
      Action_Correctly_Captured_dv = models.CharField(_('Action Correctly Captured dv'), max_length=200)
      Audit_Outcome_dv = models.CharField(_('Audit Outcome dv'), max_length=200)
      Defect_Area_Investigator_dv = models.CharField(_('Defect Area Investigator'), max_length=200)
      Document_Name_dv = models.CharField(_('Document Name dv'), max_length=200)
      Document_Type_dv = models.CharField(_('Document Type dv'), max_length=200)
      Type_of_Audit_dv = models.CharField(_('Type of Audit dv'), max_length=200)
      If_Correctly_Captured_No_dv = models.CharField(_('If Correctly Captured No dv'), max_length=200)
      Country_dv = models.CharField(_('Country dv'), max_length=200)
      Region_dv = models.CharField(_('Region dv'), max_length=200)
      Key_Driver_unique_dv = models.CharField(_('Metric name dv'), max_length=200, default=None)
      Sub_Key_Driver_unique_dv = models.CharField(_('Metric name dv'), max_length=200, default=None)
  
      def __str__(self):
          return self.Defect_Area_dv
  
  
  class auditsModel(models.Model):
      Dv_model = models.ForeignKey(
          dv_model,
          on_delete=models.CASCADE,
          null=True
      )
      # auditsModelKey = models.ForeignKey(dv_model, on_delete = models.CASCADE, default = '')
      country = models.CharField(_('country'), max_length=200, default='')
      qs_login = models.CharField(_('qs_login'), max_length=200, default='')
      Status = models.CharField(_('Status'), max_length=200, default='')
      seller_id = models.CharField(_('seller_id'), max_length=200, default='')
      Task_ID = models.CharField(_('Task_ID'), max_length=200, default='', primary_key=True)
      associate_queue = models.CharField(_('associate_queue'), max_length=200, default='')
      associate = models.CharField(_('associate'), max_length=200, default='')
      metric_wk_no = models.CharField(_('metric_wk_no'), max_length=200, default='')
      associate_action = models.CharField(_('associate_action'), max_length=200, default='')
      correct_associate_action = models.CharField(_('correct associate action'), max_length=200, default='')
      Associate_Key_Driver = models.CharField(_('Associate_Key_Driver'), max_length=200, default='')
      Sub_Key_driver = models.CharField(_('Sub_Key_driver'), max_length=200, default='')
      Defect_Area_Associate = models.CharField(_('Defect_Area_Associate'), max_length=200, default='')
      QA_Comments_on_Associate_Action = models.CharField(_('QA_Comments_on_Associate_Action'), max_length=400, default='')
      Metric_name = models.CharField(_('Metric_name'), max_length=200, default='')
  
      # SIV
      investigator_task = models.CharField(_('investigator_task'), max_length=200, default='')
      investigator_queue = models.CharField(_('investigator_queue'), max_length=200, default='')
      investigator = models.CharField(_('investigator'), max_length=200, default='')
      verification_action = models.CharField(_('verification_action'), max_length=200, default='')
      correct_investigator_action = models.CharField(_('correct investigator action'), max_length=200, default='')
      Investigator_Key_Driver = models.CharField(_('Investigator Key-Driver'), max_length=200, default='')
      Defect_Area_Investigator = models.CharField(_('Defect Area Investigator'), max_length=200, default='')
      QA_Comments_on_investigator_Action = models.CharField(_('QA Comments on investigator Action'), max_length=200,
                                                            default='')
      General_Notes = models.CharField(_('General_Notes'), max_length=200, default='')
  
      Action_correctly_captured = models.CharField(_('Action_correctly_captured'), max_length=200, default='')
      Audit_outcome = models.CharField(_('Audit_outcome'), max_length=200, default='')
      associate_resolve_date = models.CharField(_('Audit_outcome'), max_length=200, default='')
      Type_of_audit = models.CharField(_('Type_of_audit'), max_length=200, default='')
      If_data_correctly_captured = models.CharField(_('If_data_correctly_captured'), max_length=200, default='')
  
      def __str__(self):
          return f&quot;Audit: {self.country}-{self.qs_login}-{self.Status}-{self.seller_id}-{self.Task_ID}-{self.associate_queue}-{self.associate}-{self.metric_wk_no}-{self.associate_action}-{self.correct_associate_action}-{self.Associate_Key_Driver}-{self.Sub_Key_driver}-{self.Defect_Area_Associate}-{self.QA_Comments_on_Associate_Action}-{self.Metric_name}-{self.investigator_task}-{self.investigator_queue}-{self.investigator}-{self.verification_action}-{self.correct_investigator_action}-{self.Investigator_Key_Driver}-{self.Defect_Area_Investigator}-{self.QA_Comments_on_investigator_Action}-{self.General_Notes}-{self.Type_of_audit}-{self.Action_correctly_captured}-{self.Audit_outcome}-{self.associate_resolve_date}-{self.If_data_correctly_captured}&quot;
  </code></pre>
  <p>Then in the view.py i referenced the fields in the data validation(dv_model) to match the ones in the main model(auditsmodel)</p>
  <p>Views.py:</p>
  <pre><code>def auditFormPage(request, pk):
      model = auditsModel.objects.filter(qs_login=request.user)
      form = auditForm()
      
      try:
          data = auditsModel.objects.get(Task_ID=pk)
  
          form = auditForm(instance=data)
          if request.method == 'POST':
              correct_action = request.POST.get('correct_action')
              key_driver = request.POST.get('key_driver')
              defect_area = request.POST.get('defect_area')
              defect_area = str(defect_area)
              metric = request.POST.get('metric')
              QA_comment = request.POST.get('QA_comment')
              
              data.correct_associate_action = correct_action
              data.Associate_Key_Driver = key_driver
              data.Defect_Area_Associate = defect_area
              data.save()
              form.save()
      except auditsModel.DoesNotExist:
          pass
  
      context = {
          &quot;items&quot;: model,
          &quot;form&quot;: form
      }
      return render(request, &quot;main/auditform.html&quot;, context)
  </code></pre>
  <p>This is my forms.py:</p>
  <pre><code>class auditForm(ModelForm):
      class Meta:
          model = auditsModel
          fields = '__all__'
  
      # RADIO BUTTONS
      CHOICES = [('Defect', 'yes'),
                 ('No Defect', 'no')]
      CHOICES_Action_correctly_captured = [('yes', 'yes'), ('no', 'no')]
      Audit_outcome = forms.ChoiceField(choices=CHOICES, widget=RadioSelect())
      Action_correctly_captured = forms.ChoiceField(choices=CHOICES_Action_correctly_captured, widget=RadioSelect())
      #Defect_Area_Associate = forms.ModelChoiceField(queryset=dv_model.objects.values_list('Defect_Area_dv', flat=True).distinct(), widget=forms.Select)
  
      def __init__(self, *args, **kwargs):
          super(auditForm, self).__init__(*args, **kwargs)
          # Makes the field un-editable
  
          # FIELDS-----------
  
          # IPV COMMENT BOX
          self.fields['QA_Comments_on_Associate_Action'].widget = forms.Textarea(
              attrs={'class': 'form-control', 'style': 'height: 100px'})  # Sets the size of the widget
          # SIV COMMENT BOX
          self.fields['QA_Comments_on_investigator_Action'].widget = forms.Textarea(
              attrs={'class': 'form-control', 'style': 'height: 100px'})  # Sets the size of the widget
  
          # ENABLE/DISABLE FIELDS
          self.fields['country'].disabled = True
          self.fields['associate_resolve_date'].disabled = True
          self.fields['qs_login'].disabled = True
          self.fields['associate_resolve_date'].disabled = True
          self.fields['seller_id'].disabled = True
          self.fields['Task_ID'].disabled = True
          self.fields['associate_queue'].disabled = True
          self.fields['associate'].disabled = True
          self.fields['metric_wk_no'].disabled = True
          self.fields['associate_action'].disabled = True
  
          for field in self.fields:
              self.fields[field].required = False
              if field != &quot;qs_login&quot; and field != &quot;Status&quot; and field != &quot;associate_resolve_date&quot; and field != &quot;QA_Comments_on_Associate_Action&quot; and field != &quot;Audit_outcome&quot;:
                  self.fields[field].widget.attrs['class'] = &quot;form-control&quot;
  # -----------------------------------------
  </code></pre>