I am writing a script and want to fake a User with an id
(PK) of 2
var = Table(FKToUser=2)
var.save()
the problem is I'm getting :
"Table.FKToUser" must be a "User" instance
I've verified that the auth_user
has a record with id=2
How can I fake this 2
value for testing purposes?
Thank you!
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.