乌龟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 (将#修改为@)