Django Cannot assign "A1": "B1" must be a "C1" instance.

Posted Rev-RoastDuck

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django Cannot assign "A1": "B1" must be a "C1" instance.相关的知识,希望对你有一定的参考价值。

Django Cannot assign "A1": "B1" must be a "C1" instance.

原因:使用了外键

说明:如果想要给外键字段插入数据时,我们必须使用 C1 模型类的实例,而不是具体的参数值。也就是说,如果使用了外键,先实例化外键查询,然后再插入的表里面放入实例化后的外键连接。

错误示例

# 从request.session取出name的值,
# 然后把name的值插入到form对象的user_name中

form.instance.user_name = request.session["info"]["name"]

正确示例

# 通过request.session取出name的值作为索引,
# 到数据库取出外键,实例化外键查询
# 然后把把实例化的外键插入到form对象的user_name中

user_name=UserInfo.objects.get(name=request.session["info"]["name"])
form.instance.user_name = user_name

以上是关于Django Cannot assign "A1": "B1" must be a "C1" instance.的主要内容,如果未能解决你的问题,请参考以下文章

阿里云服务器配置uwsgi报错bind(): Cannot assign requested address [core/socket.c line 769]

[译]Python - socket.error: Cannot assign requested address

golang cannot assign to

FlinkFlink SQL Cannot instantiate user function cannot assign instance LinkedMap FlinkKafkaConsum

Cannot assign requested address 问题

Irssi Error [Cannot assign requested address: x.x.x.x]