微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

乌龟ORM多对多字段插入

如何解决乌龟ORM多对多字段插入

我有一个基于Fastapi和Tormise orm的应用程序。使用以下模型:

模型:

class Tratamiento(models.Model):

id = fields.UUIDField(pk=True)
nombre = fields.CharField(max_length=100)
descripcion = fields.CharField(max_length=300)
estado = fields.BooleanField()
costo = fields.IntField()


Tortoise.init_models(["db.models"],"models")
tratamiento_in = pydantic_model_creator(Tratamiento,name="TratamientoIn",exclude_readonly=True)
tratamiento_out = pydantic_model_creator(Tratamiento,name="TratamientoOut")


class Presupuesto(models.Model):

id = fields.UUIDField(pk=True)
paciente = fields.ForeignKeyField('models.Paciente',related_name="presupuesto_paciente",on_delete=fields.CASCADE)
tratamiento = fields.ManyToManyField('models.Tratamiento',related_name="presupuesto_tratamiento",on_delete=fields.CASCADE)
descripcion = fields.CharField(max_length=500)
total = fields.IntField()
fecha = fields.DateField()


Tortoise.init_models(["db.models"],"models")
presupuesto_in = pydantic_model_creator(Presupuesto,name="PresupuestoIn",exclude_readonly=True)
presupuesto_out = pydantic_model_creator(Presupuesto,name="PresupuestoOut")

我正在尝试将vuejs的一些值发布到此路由:

路线:

@router.post("/presupuestos/add",response_model=presupuesto_out)
async def post_presupuesto(presupuesto: presupuesto_in,tratamientos: List[TratamientosPydantic]):

for item in tratamientos:
    presupuesto.total += item.costo

insert_result = await Presupuesto.create(**presupuesto.dict(exclude_unset=True))
for item in tratamientos:
    tratamiento = await tratamiento_out.from_queryset_single(Tratamiento.get(id=item.tratamiento_id))
    insert_related = await insert_result.tratamiento.add(*tratamiento)

我在将值添加到多对多字段时遇到了下一个错误(创建效果很好),不知道如何解决...请帮忙!

AttributeError:类型对象'tuple'没有属性'_Meta'

路线图模式:

class TratamientosPydantic(BaseModel):
paciente_id: str
tratamiento_id: str
costo: int

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。