3

How to set username as ForeignKey in Django module. is bellow method is correct?

user = models.ForeignKey(User, db_column="user")

i cannot use ID as ForeignKey because my old db have username as ForeignKey.(I need to migrate the the old Data)

Kombuwa
  • 1,613
  • 4
  • 20
  • 35

1 Answers1

2

Use below code:

   user = models.ForeignKey(User, to_field="username")

The field on the related object that the relation is to. By default, Django uses the primary key of the related object. If you reference a different field, that field must have unique=True.

Milad Hatami
  • 1,494
  • 13
  • 18