A have two model tables which contains data.
class A(models.Model):
id = models.PositiveIntegerField(primary_key=True)
#some other fields
other_id = models.IntegerField(default=0, null=True)
And the second class
class B(models.Model):
id = models.PositiveIntegerField(primary_key=True)
#some other fields
Now I want field A.other_id
to become a ForeignKey
related to field B.id
.
I cannot use SQL. I know it is supposed to be something like:
b = models.ForeignKey(B, db_column="other_id")
but it seems to not work.
Did you try to_field
option?
You can do:
b = models.ForeignKey(B, to_field="other_id")
However, the catch is that field other_id
must satisfy unique=True
.