问题描述:

I have a model which has a ForeignKey field, and I wish to change its value in a model instance, I tried something like this:

#object and user are model instances, owner is a FK field

object.owner = user

This raises a ValueError : "object.owner is not an instance of User"

Is there a workaround for this?

网友答案:

user should be a correct instance of User, and if you just created it instead of retrieved it from the database, it should be save()-d before you save object.

网友答案:

Make sure that user is an instance of the same User that was used in defining the ForeignKey.

相关阅读:
Top