Python Django查询集

Python Django查询集,python,django,django-queryset,Python,Django,Django Queryset,我在django玩querysets 我所期待的是保存一个新的外国产品或项目,但我无法实现它 壳 型号.py 我做了什么坏事 我正在尝试为db中已经存在的客户创建一个新产品。我想你就快到了。您需要调用新产品的save方法来保存到数据库,并且要检索相关的客户端对象,您应该getnotfilter,这样您就拥有了对象本身,而不是对象列表(或QuerySet): detalle=Datos\u empresa\u DB.objects.get(pk=58) #

我在django玩querysets

我所期待的是保存一个新的外国产品或项目,但我无法实现它

型号.py

我做了什么坏事


我正在尝试为db中已经存在的客户创建一个新产品。

我想你就快到了。您需要调用新产品的
save
方法来保存到数据库,并且要检索相关的客户端对象,您应该
get
not
filter
,这样您就拥有了对象本身,而不是对象列表(或QuerySet):

detalle=Datos\u empresa\u DB.objects.get(pk=58)
#                                  ^^^
resp=Datos\u equipmo\u DB(equipmo='dell-labtop',marca='dell',modelo='432423',for neo\u Datos\u empresa\u DB=detalle)

#保存在模型的相关字段中,您应该包括您的模型定义,这样我们就可以知道表的形状。或者,或者,
resp=Datos\u empresa\u DB.objects.create(equipo='dell-labtop',marca='dell',modelo='432423',detalle=detalle)
@spectras是的,那也是:)你们确定吗?@elquitecto在模型实例化中将Datos\u empresa\u DB的
更改为

from applaboratorio.models import Datos_empresa_DB, Datos_equipo_DB

detalle = Datos_empresa_DB.objects.filter(pk=58)

resp = Datos_equipo_DB(equipo='dell-labtop',marca='dell', modelo='432423',Foraneo_Datos_empresa_DB = detalle)
class Datos_empresa_DB(models.Model):
    nombre = models.CharField(max_length=150)
    empresa = models.CharField(max_length=150)

class Datos_equipo_DB(models.Model):
    Foraneo_Datos_empresa_DB = models.ForeignKey(Datos_empresa_DB)
    equipo = models.CharField(max_length=300)
    marca = models.CharField(max_length=300)
    modelo = models.CharField(max_length=300)
detalle = Datos_empresa_DB.objects.get(pk=58)
#                                  ^^^
resp = Datos_equipo_DB(equipo='dell-labtop',marca='dell', modelo='432423',Foraneo_Datos_empresa_DB =detalle)
#                                          Save on model's related field <-^^^^^^^
resp.save()