我正在将 fastapi 与 tortoise orm 一起使用,当我想添加购物车时出现此错误
错误
如果值而不是值。_saved_in_db:AttributeError:'int'对象没有属性'_saved_in_db'
楷模
class Cart(BaseModel):
user = fields.ForeignKeyField('models.User', related_name='user_cart')
product = fields.ForeignKeyField(
'models.Product', related_name='product_cart')
quantity = fields.IntField(default=0)
class Meta:
table = 'carts'
ordering = ('-created',)
图式
class CartCreate(BaseModel):
product_id: int
quantity: int = Field(..., ge=0, le=10)
class Cart(BaseModel):
id: int
class Config:
orm_mode = True
路线
@router.post('/create', status_code=status.HTTP_201_CREATED)
async def create_cart(
cart: CartCreate,
user: User = Depends(get_current_user)
) -> CartSchema:
cart_obj = await Cart.create(
user=user.id,
product=cart.product_id,
quantity=cart.quantity
)
return CartSchema.from_orm(cart_obj)
我应该怎么办?