[Answer]-AttributeError when establishing a foreign key using the to_field option

1👍

From docs:

The field on the related object that the relation is to. By default, Django uses the primary key of the related object.

As we know PK is unique. Try setting order_number as unique in WorldEaseConsignee model:

class WorldEaseConsignee(models.Model):
    order_number = models.IntegerField(
        null=False,
        blank=False,
        unique=True
    )

Also, set to_field value as follow:

class WorldEaseInvoice(models.Model):
        order_number_id = models.ForeignKey(
            WorldEaseConsignee,
            to_field='order_number',
        )
👤Gocht

Leave a comment