title: Django: How to pass string name instead of foreign id when creating an object
body: <p>I have two table, <strong>ShippingAddress</strong> and <strong>Invoice</strong> table linked via <code>ForeignKey</code>. I have created a signals file that automatically creates <strong>Invoice</strong> instance whenever a new <strong>ShippingAddress</strong> instance is created.</p>
  <p>My problem is, I can't directly add a new Invoice instance by using the string name rather than the ForeignKey id without getting an error</p>
  <blockquote>
  <p>&quot;shipping_address&quot;: [
  &quot;Incorrect type. Expected pk value, received str.&quot;
  ]</p>
  </blockquote>
  <p>How do i go about using string name of the field <code>shipping_address</code> found in <strong>Invoice</strong> table while maintaining the relationship between the two tables.</p>
  <p><strong>ShippingAddress</strong> model</p>
  <pre><code>class ShippingAddress(models.Model):
  
      customer = models.ForeignKey(Customer, on_delete=models.CASCADE, related_name=&quot;customer_ship_address&quot;)
      address = models.CharField(max_length=50, blank=True)
      zip_code = models.CharField(max_length=12, blank=True)
  
      def __str__(self):
          return self.customer.name
  </code></pre>
  <p><strong>Invoice</strong> model</p>
  <pre><code>class Invoice(models.Model):
  
      shipping_address = models.ForeignKey(ShippingAddress, on_delete=models.CASCADE, related_name=&quot;ship_address_name&quot;)
      invoice_id = models.CharField(max_length=50, unique=True, default=increment_invoice_number)
      product = models.CharField(max_length=50, blank=True)
      date = models.DateField(default=datetime.date.today)
  
      def __str__(self):
          return self.shipping_address.name
  </code></pre>
  <p><strong>signal</strong> file</p>
  <pre><code>@receiver(post_save, sender=ShippingAddress)
  def create_invoice(sender, instance, created, **kwargs):
      if created:
          Invoice.objects.create(shipping_address=instance)
  </code></pre>
  <p><strong>views.py</strong> file</p>
  <pre><code>class InvoiceListCreateView(ListCreateAPIView):
  
      serializer_class = InvoiceSerializer
      queryset = Invoice.objects.all().order_by('-date')
  </code></pre>