tej.tan
tej.tan

Reputation: 4177

How to mention other field as Foreign Key in Django Model

In my model i have

book_id PK
book_number as unique field

Now in my Author model i want to set up

book_number as Foreign key not book_id

Is this possible

Upvotes: 1

Views: 466

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599480

to_field.

Upvotes: 3

lprsd
lprsd

Reputation: 87077

ForeignKey refers to an entity by it's primary key, by database design. You should probably not do it although, you might be able to fake it within django by lieing within Model.Meta what the primary key is.

The right approach seems to me, is to make the book_number, the PK, if you are confident there aren't any repeats, and may be still retain the serial type for book_id.

Upvotes: 0

Related Questions