how to create ForeignKey for username (not userid) in django models.py

Viewed 285

I created the following fieldmodels.py

added_by = models.ForeignKey(
        settings.AUTH_USER_MODEL, on_delete=models.CASCADE)

and it contained the user Id , but how to make it contain the username instead.

1 Answers

ForeightKey key always reference with an id.If you want to display the username you can do something like added_by.username

Related