title: how to insert foreign key data through Django Model form
body: <p>How to insert foreign key data through form. Please help . what am I doing wrong. So I have two models as follows : </p>
  
  <pre><code>class Genre(models.Model):
      """Model representing a book genre."""
      name = models.CharField(max_length=200, help_text='Enter a book genre (e.g. Science Fiction)')
  
      def __str__(self):
          """String for representing the Model object."""
          return self.name
  
      ass UserItem(models.Model):
          name= models.CharField(max_length = 50, null=True)
          genre = models.ForeignKey(Genre, on_delete=models.SET_NULL, null=True)
  </code></pre>
  
  <p><code>Forms.py</code> : I want to create form to store Item data. Not able to reference Genre and enter value through form.</p>
  
  <pre><code>    class ItemForm(forms.ModelForm):
          name = forms.CharField()
          genre = forms.CharField()
  
          class Meta:
              model = UserItem
              fields = ['name', 'genre' ]
  </code></pre>
  
  <p><code>Views.py</code> </p>
  
  <pre><code>    def item(request):
          if request.method == 'POST':
              genre_obj = Genre.objects.get(name=request.POST.get('name'))
              print("Inside post")
              item_form = ItemForm(request.POST)
              if item_form.is_valid():
                  print("valid form")
                  item_form_obj = ItemForm.save(commit=False)
                  item_form_obj.genre = genre_obj
                  item_form_obj.save()
                  return HttpResponseRedirect(reverse('file_upload') )
              else:
                  print("invalid form")
              return HttpResponseRedirect(reverse('file_list') )
  </code></pre>