1👍
✅
Assuming that Table
is your model and FKToUser
is a foreign key, there are two ways. The first is to set the FKToUser_id
attribute to 2
and save the model. The other is to fetch the user and set the FKToUser
attribute to the right user model instance.
This is also basically how a foreign key works. The actual column in the database is the FKToUser_id
column, and it’s a simple Integer foreign key to an id in another column. Django magic makes it possible to automatically retrieve the right instance by accessing FKToUser
, and to set the right value by assigning a model instance to FKToUser
.
👤knbk
Source:stackexchange.com