RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1253614
Accepted
Spartak
Spartak
Asked:2022-03-09 20:55:35 +0000 UTC2022-03-09 20:55:35 +0000 UTC 2022-03-09 20:55:35 +0000 UTC

FastApi 值不是有效的字典

  • 772

我描述了从那里设置的键值对传输大量 json 的 api 方法。我不想在BaseModel中描述模型的每个字段,因为大约有100个,我已经在Plan类中描述了它们。我只想在调用该方法并在那里传递 json 时创建我的自定义 Plan 类的对象。

from fastapi import FastAPI
from pydantic import BaseModel, Json
#from models import Plan
from typing import Dict

class Item(BaseModel):

    params: Json[Dict[str, float]]

app = FastAPI()

@app.post("/setPlan")
async def setPlan(params : Item):
    #plan = Plan.Plan(params.__dict__)
    #return plan.__dict__
    return {"params": params}

当我尝试发送请求并传递 json 时:

import requests
import json

params = {
    "params" : {
        'point_break_even': float(2550000),
        'days_in_mount': float(22),
        'total_proceeds': float(2550000),

        'proceed_input_percent': float(21.95),

        'hookah_average_check': float(333),
        'hookah_proceeds_percent': float(5.3),

        'bar_proceeds_percent': float(65.72),
        'bar_people_count': float(11339),
        'bar_average_check': float(500),

        'kitchen_proceeds_percent': float(7.03),
        'kitchen_average_check': float(329),

        'bar_foodcost': float(-51.5),
        'bar_products_write_off_percent': float(-0.04),
        'bar_surplus_percent': float(1.5),
        'bar_failure_percent': float(-0.5),

        'kitchen_foodcost_percent': float(-45),
        'kitchen_products_write_off_percent': float(-1.5),
        'kitchen_surplus_percent': float(1.0),
        'kitchen_failure_percent': float(-1.0),

        'fot_labor_zp_povar_percent': float(-25.0),
        'fot_labor_povar_compensation_rub': int(0),
        'fot_labor_povar_mat_help_rub': int(0),

        'fot_labor_admin_zp_rub': int(-40000),
        'fot_labor_admin_zp_bonus_rub': int(0),
        'fot_labor_admin_zp_compensation_rub': int(0),
        'fot_labor_admin_zp_mat_help_rub': int(0),

        'fot_zp_zvukar': int(0),
        'fot_zp_zavhoz': int(0),
        'fot_zp_officiants': int(0),
        'fot_zp_cashiers': int(0),
        'fot_zp_hostes': int(0),
        'fot_zp_barmans': int(0),
        'total_loosers_zp': int(-300000),

        'fot_zp_manager': int(-40000),
        'fot_zp_bu_calc_kadr': int(-103000),
        'fot_zp_admin_po_rejimy': int(-132000),

        'personal_apartments_rent_rub': int(0),
        'personal_food_cost_percent': float(-0.68),
        'personal_transport_cost_rub': int(0),
        'personal_medicaments_medosmotr_rub': int(0),
        'personal_registration_in_ufms_rub': int(0),

        'rental_of_premises_rub': int(-500000),
        'communal_rent': int(0),
        'wanish_cost_percent': float(-0.16),
        'parking_coupons_rub': int(0),
        'room_maintenance_rub': int(0),
        'dizenfection_rub': int(-3003),
        'kovri_ot_gryazi_rub': int(0),
        'to_kts_rub': int(-7000),
        'garbage_removal_rub': int(0),

        'premises_capital_remont_rub': int(0),
        'premises_cosmetical_remont_rub': int(-15000),
        'furniture_updating_rub': int(-10000),
        'equipment_updating_rub': int(0),
        'equipment_remont_service_rub': int(0),
        'air_conditioning_remont_service_rub': int(0),
        'ventilation_remont_service_rub': int(0),
        'lamp_remont_service_rub': int(0),
        'electric_remont_service_rub': int(0),
        'canalization_remont_service_rub': int(0),
        'posuda_inventar_updating_percent': float(-0.35),
        'kitchen_inventar_service_rub': int(-7000),

        'orgtechnic_and_cassovie': int(-2500),
        'packaging_rub': int(0),
        'hoztovars_percent': float(-0.4),
        'prochie_rashodniki_percent': float(-0.14),

        'stationery_rub': int(-2500),

        # Прочие расходные материалы (значки, браслеты, телефоны POS-материалы, наклейки, дым, номерки, пейджеры)
        'znachki_rub': int(0),
        'phone_rub': int(0),
        'nomerki_rub': int(0),
        'pos_packages_rub': int(0),
        'control_brasslets_rub': int(0),
        'eticet_pistol_rub': int(0),
        'button_call': int(0),
        'padger': int(0),
        'smoke': int(0),
        'stickers': int(0),

        # Расходы на связь
        'internet_connection_rub': int(-1500),
        'post_office_rub': int(0),
        'phone_connection_rub': int(-1000),

        'transport_rub': int(0),
        'it_and_po_rub': int(-10500),
        'kkm_rub': int(-1400),
        'sas_standart_lab_issled_rub': int(0),
        'musical_design_of_music_rub': int(-12560),
        'sbis_ofd_rub': int(-1620),
        'cctv_and_control_rub': int(0),

        # Финансовые расходы
        'expenses_acquiring_percent': float(-1.05),
        'expenses_banking_service_percent': float(-0.09),

        # налоги и отчисления
        'income_tax_usn_envd_rub': int(0),
        'soc_fonds_percent': float(-0.93),
        'taxes_fees_fines_rub': int(0),

        # Реклама и маркетинг
        'fot_adv_specialists_rub': int(-128000),
        'marketing_events_percent': float(-5.0),

        # P3
        'p3_accountant_rub': int(-30000),
        'p3_royalty_rub': int(0),
        'p3_xx_rub': int(0)
    }
}

r = requests.post('http://127.0.0.1:8000/setPlan', json = json.dumps(params))
print(r.json())

然后我收到以下错误:{'detail': [{'loc': ['body'], 'msg': 'value is not a valid dict', 'type': 'type_error.dict'}]}

通过生成的方法停靠

python
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Spartak
    2022-03-10T07:02:27Z2022-03-10T07:02:27Z

    代替

    r = requests.post('http://127.0.0.1:8000/setPlan', json = json.dumps(params))
    

    像这样:

    r = requests.post('http://127.0.0.1:8000/setPlan', json = params)
    

    它奏效了,你不需要做任何转储,我们立即将字典推到那里。该方法有效,我没有正确访问它。

    • 1

相关问题

  • 是否可以以某种方式自定义 QTabWidget?

  • telebot.anihelper.ApiException 错误

  • Python。检查一个数字是否是 3 的幂。输出 无

  • 解析多个响应

  • 交换两个数组的元素,以便它们的新内容也反转

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5