How to set ForeignKey attribute knowing just the attribute name and related object's ID?

Viewed 51

Is there a way to set ForeignKey attribute value knowing just the name of the attribute and the ID of the ForeignKey related object?

I tried to do this using setattr():

setattr(instance, 'product', 41)

The result is:

Cannot assign "41": "MyModel.product" must be a "Product" instance. 
0 Answers
Related