[Answered ]-Django Primary key acting as a foreign key

2👍

You could try to use the to_field and db_column options.

class B(models.Model):
    name = models.ForeignKeyField(A, to_field="name", db_column="name")

Once you have created the foreign key, you can access the value and related instance as follows:

>>> b = B.objects.get(id=1)
>>> b.name_id # the value stored in the 'name' database column
>>> b.name # the related 'A' instance

reference:
this answer

Leave a comment