title: Django ForeignKey Model Form
body: <p>I'm newbie on Django. I have two model and one of this model have Foreign Key. I'm using Model Form in forms and when I fill the form my foreign key field return null. What I want is when I fill the form foreign key field, fill according to the pointed out by the foreign key.</p>
  
  <p>Models:</p>
  
  <pre><code>class customerInfo(models.Model):
  customerName = models.CharField(max_length = 50)
  customerContent = models.TextField(max_length = 50)
  createdDate= models.DateTimeField(auto_now_add = True)
  
  def __str__(self):
      return self.customerName
  
  class productInfo(models.Model):
      username = models.CharField(max_length = 50)
      passwd = models.CharField(max_length = 50)
      destIp = models.CharField(max_length = 50)
      hostname = models.CharField(max_length = 50)
      productName = models.CharField(max_length = 50)
      customer = models.ForeignKey(customerInfo,on_delete = models.CASCADE,null=True)
  
      def __str__(self):
          return self.productName
  </code></pre>
  
  <p>Forms:</p>
  
  <pre><code>class customerForm(forms.ModelForm):
      class Meta:
          model = customerInfo
          fields = (
                  "customerName",
          )
  
      class addProductForm(forms.ModelForm):
          class Meta:
              model = productInfo
              fields = (
                      "productName",
                      )
  
  
      class productInfoForm(forms.ModelForm):
              class Meta:
                      model = productInfo
                      fields = (
                              "username",
                              "passwd",
                              "destIp",
                              "hostname",
                      )
  </code></pre>
  
  <p>Views:</p>
  
  <pre><code>@login_required(login_url = "/")
  def addCustomer(request):
      form = customerForm(request.POST or None)
      content = {"form" : form,}
      if form.is_valid():
          form.save()
          customerName = form.cleaned_data['customerName']
          return redirect("addproduct")
  
      else:
          return render(request,"addcustomer.html",content)
  
  @login_required(login_url = "/")
  def addProduct(request):
      form = addProductForm(request.POST or None)
      content = {"form" : form}
      if form.is_valid():
          global productName
          productName = form.cleaned_data['productName']
          return redirect("addproductinfo")
      return render(request,"addproduct.html",content)
  
  @login_required(login_url = "/")
  def addProductInfo(request):
      form = productInfoForm(request.POST or None)
      content = {"form" : form}
      if form.is_valid():
          p = form.save(commit = False)
          p.productName = productName
          p.save()
          return redirect("customer")
      return render(request,"addproductinfo.html",content)
  </code></pre>
  
  <p>As a result, I want to see the customer's products when I click on the customer name. Not all products.
  Before I can do that, the customer id fields needs to be full.
  I hope you understood me.</p>