如何在Fastapi中声明Pydantic Basemodel以在其一个键中接收有效的JSON对象?
客户端应用程序正在使用发布
方法将数据发送到服务器。数据基本上具有4个键值对,其中第二个键的值是有效的JSON对象,其中包含多个键值配对。目前看起来像这样:
{
"name": "string",
"content": {{"resolution":"1","symbol_type":"stock","exchange":"NSE","listed_exchange":"","symbol":"NSE:SBIN-EQ","short_name":"SBIN","legs":"[{\"symbol\":\"SBIN\",\"pro_symbol\":\"SBIN\"}]","name":"asdwa","description":"","charts_symbols":"{\"1\":{\"symbol\":\"NSE:SBIN-EQ\"}}","is_realtime":"1","content":"{\"name\":\"asdwa\",\"layout\":\"s\",\"charts\":[{\"panes\":[{\"sources\":[{\"type\":\"MainSeries\",\"id\":\"RGTnHz\",\"state\":{\"style\":1,\"esdShowDividends\":true,\"esdShowSplits\":true,\"esdShowEarnings\":true,\"esdShowBreaks\":false,\"esdFlagSize\":2,\"showCountdown\":false,\"bidAsk\":{\"visible\":false,\"lineStyle\":1,\"lineWidth\":1,\"bidLineColor\":\"#2962FF\",\"askLineColor\":\"#EF5350\"},\"prePostMarket\":{\"visible\":true,\"lineStyle\":1,\"lineWidth\":1,\"preMarketColor\":\"#fb8c00\",\"postMarketColor\":\"#2962FF\"},\"highLowAvgPrice\":{\"highLowPriceLinesVisible\":false,\"highLowPriceLabelsVisible\":false,\"averageClosePriceLineVisible\":false,\"averageClosePriceLabelVisible\":false},\"showInDataWindow\":true,\"visible\":true,\"showPriceLine\":true,\"priceLineWidth\":1,\"priceLineColor\":\"\",\"baseLineColor\":\"#B2B5BE\",\"showPrevClosePriceLine\":false,\"prevClosePriceLineWidth\":1,\"prevClosePriceLineColor\":\"rgba( 85, 85, 85, 1)\",\"minTick\":\"default\",\"dividendsAdjustment\":{},\"sessionId\":\"regular\",\"sessVis\":false,\"statusViewStyle\":{\"fontSize\":16,\"showExchange\":true,\"showInterval\":true,\"symbolTextSource\":\"description\"},\"candleStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"drawWick\":true,\"drawBorder\":true,\"borderColor\":\"#378658\",\"borderUpColor\":\"#26a69a\",\"borderDownColor\":\"#ef5350\",\"wickColor\":\"#737375\",\"wickUpColor\":\"#26a69a\",\"wickDownColor\":\"#ef5350\",\"barColorsOnPrevClose\":false,\"drawBody\":true},\"hollowCandleStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"drawWick\":true,\"drawBorder\":true,\"borderColor\":\"rgba( 55, 134, 88, 1)\",\"borderUpColor\":\"#26a69a\",\"borderDownColor\":\"#ef5350\",\"wickColor\":\"rgba( 115, 115, 117, 1)\",\"wickUpColor\":\"#26a69a\",\"wickDownColor\":\"#ef5350\",\"drawBody\":true},\"haStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"drawWick\":true,\"drawBorder\":true,\"borderColor\":\"rgba( 55, 134, 88, 1)\",\"borderUpColor\":\"#26a69a\",\"borderDownColor\":\"#ef5350\",\"wickColor\":\"rgba( 115, 115, 117, 1)\",\"wickUpColor\":\"#26a69a\",\"wickDownColor\":\"#ef5350\",\"showRealLastPrice\":false,\"barColorsOnPrevClose\":false,\"inputs\":{},\"inputInfo\":{},\"drawBody\":true},\"barStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"barColorsOnPrevClose\":false,\"dontDrawOpen\":false,\"thinBars\":true},\"hiloStyle\":{\"color\":\"#2962FF\",\"showBorders\":true,\"borderColor\":\"#2962FF\",\"showLabels\":true,\"labelColor\":\"#2962FF\",\"fontSize\":7,\"drawBody\":true},\"lineStyle\":{\"color\":\"#2962FF\",\"linestyle\":0,\"linewidth\":2,\"priceSource\":\"close\",\"styleType\":2},\"areaStyle\":{\"color1\":\"rgba(41, 98, 255, 0.28)\",\"color2\":\"#2962FF\",\"linecolor\":\"#2962FF\",\"linestyle\":0,\"linewidth\":2,\"priceSource\":\"close\",\"transparency\":100},\"renkoStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"borderUpColor\":\"#26a69a\",\"borderDownColor\":\"#ef5350\",\"upColorProjection\":\"rgba( 169, 220, 195, 1)\",\"downColorProjection\":\"rgba( 245, 166, 174, 1)\",\"borderUpColorProjection\":\"rgba( 169, 220, 195, 1)\",\"borderDownColorProjection\":\"rgba( 245, 166, 174, 1)\",\"wickUpColor\":\"#26a69a\",\"wickDownColor\":\"#ef5350\",\"inputs\":{\"source\":\"close\",\"sources\":\"Close\",\"boxSize\":3,\"style\":\"ATR\",\"atrLength\":14,\"wicks\":true},\"inputInfo\":{\"source\":{\"name\":\"source\"},\"sources\":{\"name\":\"Source\"},\"boxSize\":{\"name\":\"Box size\"},\"style\":{\"name\":\"Style\"},\"atrLength\":{\"name\":\"ATR length\"},\"wicks\":{\"name\":\"Wicks\"}}},\"pbStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"borderUpColor\":\"#26a69a\",\"borderDownColor\":\"#ef5350\",\"upColorProjection\":\"rgba( 169, 220, 195, 1)\",\"downColorProjection\":\"rgba( 245, 166, 174, 1)\",\"borderUpColorProjection\":\"rgba( 169, 220, 195, 1)\",\"borderDownColorProjection\":\"rgba( 245, 166, 174, 1)\",\"inputs\":{\"source\":\"close\",\"lb\":3},\"inputInfo\":{\"source\":{\"name\":\"Source\"},\"lb\":{\"name\":\"Number of line\"}}},\"kagiStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"upColorProjection\":\"rgba( 169, 220, 195, 1)\",\"downColorProjection\":\"rgba( 245, 166, 174, 1)\",\"inputs\":{\"source\":\"close\",\"style\":\"ATR\",\"atrLength\":14,\"reversalAmount\":1},\"inputInfo\":{\"source\":{\"name\":\"Source\"},\"style\":{\"name\":\"Style\"},\"atrLength\":{\"name\":\"ATR length\"},\"reversalAmount\":{\"name\":\"Reversal amount\"}}},\"pnfStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"upColorProjection\":\"rgba( 169, 220, 195, 1)\",\"downColorProjection\":\"rgba( 245, 166, 174, 1)\",\"inputs\":{\"sources\":\"Close\",\"reversalAmount\":3,\"boxSize\":1,\"style\":\"ATR\",\"atrLength\":14,\"oneStepBackBuilding\":false},\"inputInfo\":{\"sources\":{\"name\":\"Source\"},\"boxSize\":{\"name\":\"Box size\"},\"reversalAmount\":{\"name\":\"Reversal amount\"},\"style\":{\"name\":\"Style\"},\"atrLength\":{\"name\":\"ATR length\"},\"oneStepBackBuilding\":{\"name\":\"One step back building\"}}},\"baselineStyle\":{\"baselineColor\":\"rgba( 117, 134, 150, 1)\",\"topFillColor1\":\"rgba( 38, 166, 154, 0.28)\",\"topFillColor2\":\"rgba( 38, 166, 154, 0.05)\",\"bottomFillColor1\":\"rgba( 239, 83, 80, 0.05)\",\"bottomFillColor2\":\"rgba( 239, 83, 80, 0.28)\",\"topLineColor\":\"rgba( 38, 166, 154, 1)\",\"bottomLineColor\":\"rgba( 239, 83, 80, 1)\",\"topLineWidth\":2,\"bottomLineWidth\":2,\"priceSource\":\"close\",\"transparency\":50,\"baseLevelPercentage\":50},\"rangeStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"thinBars\":true,\"upColorProjection\":\"rgba( 169, 220, 195, 1)\",\"downColorProjection\":\"rgba( 245, 166, 174, 1)\",\"inputs\":{\"range\":10,\"phantomBars\":false},\"inputInfo\":{\"range\":{\"name\":\"Range\"},\"phantomBars\":{\"name\":\"Phantom bars\"}}},\"symbol\":\"NSE:SBIN-EQ\",\"shortName\":\"SBIN\",\"timeframe\":\"\",\"onWidget\":false,\"interval\":\"1\",\"unitId\":null,\"currencyId\":null,\"priceAxisProperties\":{\"autoScale\":true,\"autoScaleDisabled\":false,\"lockScale\":false,\"percentage\":false,\"percentageDisabled\":false,\"log\":false,\"logDisabled\":false,\"alignLabels\":true,\"isInverted\":false,\"indexedTo100\":false}},\"zorder\":0,\"haStyle\":{\"studyId\":\"BarSetHeikenAshi@tv-basicstudies-60\"},\"renkoStyle\":{\"studyId\":\"BarSetRenko@tv-prostudies-40\"},\"pbStyle\":{\"studyId\":\"BarSetPriceBreak@tv-prostudies-34\"},\"kagiStyle\":{\"studyId\":\"BarSetKagi@tv-prostudies-34\"},\"pnfStyle\":{\"studyId\":\"BarSetPnF@tv-prostudies-34\"},\"rangeStyle\":{\"studyId\":\"BarSetRange@tv-basicstudies-72\"}},{\"type\":\"study_Volume\",\"id\":\"bl7jkV\",\"state\":{\"styles\":{\"vol\":{\"linestyle\":0,\"linewidth\":1,\"plottype\":5,\"trackPrice\":false,\"transparency\":50,\"visible\":true,\"color\":\"#000080\",\"histogramBase\":0,\"joinPoints\":false,\"title\":\"Volume\"},\"vol_ma\":{\"linestyle\":0,\"linewidth\":1,\"plottype\":0,\"trackPrice\":false,\"transparency\":0,\"visible\":false,\"color\":\"#2196f3\",\"histogramBase\":0,\"joinPoints\":false,\"title\":\"Volume MA\"}},\"palettes\":{\"volumePalette\":{\"colors\":{\"0\":{\"color\":\"#EF5350\",\"width\":1,\"style\":0,\"name\":\"Falling\"},\"1\":{\"color\":\"#26A69A\",\"width\":1,\"style\":0,\"name\":\"Growing\"}}}},\"inputs\":{\"showMA\":false,\"length\":20,\"col_prev_close\":false},\"precision\":\"default\",\"bands\":{},\"area\":{},\"graphics\":{},\"showInDataWindow\":true,\"visible\":true,\"showStudyArguments\":true,\"intervalsVisibilities\":{\"ticks\":true,\"seconds\":true,\"secondsFrom\":1,\"secondsTo\":59,\"minutes\":true,\"minutesFrom\":1,\"minutesTo\":59,\"hours\":true,\"hoursFrom\":1,\"hoursTo\":24,\"days\":true,\"daysFrom\":1,\"daysTo\":366,\"weeks\":true,\"weeksFrom\":1,\"weeksTo\":52,\"months\":true,\"monthsFrom\":1,\"monthsTo\":12,\"ranges\":true},\"plots\":{\"0\":{\"id\":\"vol\",\"type\":\"line\"},\"1\":{\"id\":\"volumePalette\",\"palette\":\"volumePalette\",\"target\":\"vol\",\"type\":\"colorer\"},\"2\":{\"id\":\"vol_ma\",\"type\":\"line\"}},\"_metainfoVersion\":52,\"isTVScript\":false,\"isTVScriptStub\":false,\"is_hidden_study\":false,\"description\":\"Volume\",\"shortDescription\":\"Volume\",\"is_price_study\":false,\"id\":\"Volume@tv-basicstudies\",\"format\":{\"type\":\"volume\"},\"description_localized\":\"Volume\",\"shortId\":\"Volume\",\"packageId\":\"tv-basicstudies\",\"version\":\"1\",\"fullId\":\"Volume@tv-basicstudies-1\",\"productId\":\"tv-basicstudies\",\"name\":\"Volume@tv-basicstudies\",\"_serverMetaInfoVersion\":52},\"zorder\":-10000,\"ownFirstValue\":null,\"metaInfo\":{\"palettes\":{\"volumePalette\":{\"colors\":{\"0\":{\"name\":\"Falling\"},\"1\":{\"name\":\"Growing\"}}}},\"inputs\":[{\"id\":\"showMA\",\"name\":\"show MA\",\"defval\":false,\"type\":\"bool\",\"isHidden\":true},{\"id\":\"length\",\"name\":\"MA Length\",\"defval\":20,\"type\":\"integer\",\"min\":1,\"max\":2000},{\"defval\":false,\"id\":\"col_prev_close\",\"name\":\"Color based on previous close\",\"type\":\"bool\"}],\"plots\":[{\"id\":\"vol\",\"type\":\"line\"},{\"id\":\"volumePalette\",\"palette\":\"volumePalette\",\"target\":\"vol\",\"type\":\"colorer\"},{\"id\":\"vol_ma\",\"type\":\"line\"}],\"graphics\":{},\"defaults\":{\"styles\":{\"vol\":{\"linestyle\":0,\"linewidth\":1,\"plottype\":5,\"trackPrice\":false,\"transparency\":50,\"visible\":true,\"color\":\"#000080\"},\"vol_ma\":{\"linestyle\":0,\"linewidth\":1,\"plottype\":0,\"trackPrice\":false,\"transparency\":0,\"visible\":false,\"color\":\"#2196F3\"}},\"palettes\":{\"volumePalette\":{\"colors\":{\"0\":{\"color\":\"#EF5350\",\"width\":1,\"style\":0},\"1\":{\"color\":\"#26A69A\",\"width\":1,\"style\":0}}}},\"inputs\":{\"showMA\":false,\"length\":20,\"col_prev_close\":false}},\"_metainfoVersion\":52,\"isTVScript\":false,\"isTVScriptStub\":false,\"is_hidden_study\":false,\"styles\":{\"vol\":{\"title\":\"Volume\",\"histogramBase\":0},\"vol_ma\":{\"title\":\"Volume MA\",\"histogramBase\":0}},\"description\":\"Volume\",\"shortDescription\":\"Volume\",\"is_price_study\":false,\"id\":\"Volume@tv-basicstudies-1\",\"format\":{\"type\":\"volume\"},\"description_localized\":\"Volume\",\"shortId\":\"Volume\",\"packageId\":\"tv-basicstudies\",\"version\":\"1\",\"fullId\":\"Volume@tv-basicstudies-1\",\"productId\":\"tv-basicstudies\",\"name\":\"Volume@tv-basicstudies\",\"_serverMetaInfoVersion\":52}}],\"leftAxisesState\":[],\"rightAxisesState\":[{\"state\":{\"id\":\"AEh8VAL7anFf\",\"m_priceRange\":{\"m_maxValue\":505.6,\"m_minValue\":494},\"m_isAutoScale\":true,\"m_isPercentage\":false,\"m_isIndexedTo100\":false,\"m_isLog\":false,\"m_isLockScale\":false,\"m_isInverted\":false,\"m_height\":641.6,\"m_topMargin\":0.1,\"m_bottomMargin\":0.08,\"alignLabels\":true},\"sources\":[\"RGTnHz\"]}],\"overlayPriceScales\":{\"bl7jkV\":{\"id\":\"aYA6FWoeiTkP\",\"m_priceRange\":{\"m_maxValue\":298353,\"m_minValue\":0},\"m_isAutoScale\":true,\"m_isPercentage\":false,\"m_isIndexedTo100\":false,\"m_isLog\":false,\"m_isLockScale\":false,\"m_isInverted\":false,\"m_height\":641.6,\"m_topMargin\":0.1,\"m_bottomMargin\":0.08,\"alignLabels\":true}},\"stretchFactor\":2000,\"mainSourceId\":\"RGTnHz\",\"priceScaleRatio\":null}],\"timeScale\":{\"m_barSpacing\":6,\"m_rightOffset\":10},\"chartProperties\":{\"paneProperties\":{\"backgroundType\":\"solid\",\"background\":\"#ffffff\",\"backgroundGradientStartColor\":\"#ffffff\",\"backgroundGradientEndColor\":\"#ffffff\",\"vertGridProperties\":{\"color\":\"rgba(42, 46, 57, 0.06)\",\"style\":0},\"horzGridProperties\":{\"color\":\"rgba(42, 46, 57, 0.06)\",\"style\":0},\"crossHairProperties\":{\"color\":\"#9598A1\",\"style\":2,\"transparency\":0,\"width\":1},\"topMargin\":10,\"bottomMargin\":8,\"axisProperties\":{\"autoScale\":true,\"autoScaleDisabled\":false,\"lockScale\":false,\"percentage\":false,\"percentageDisabled\":false,\"indexedTo100\":false,\"log\":false,\"logDisabled\":false,\"alignLabels\":true,\"isInverted\":false},\"legendProperties\":{\"showStudyArguments\":true,\"showStudyTitles\":true,\"showStudyValues\":true,\"showSeriesTitle\":true,\"showSeriesOHLC\":true,\"showLegend\":true,\"showBarChange\":true,\"showBackground\":true,\"backgroundTransparency\":50,\"wrapText\":false}},\"scalesProperties\":{\"backgroundColor\":\"#ffffff\",\"lineColor\":\"rgba(42, 46, 57, 0.14)\",\"textColor\":\"#131722\",\"fontSize\":12,\"scaleSeriesOnly\":false,\"showSeriesLastValue\":true,\"seriesLastValueMode\":1,\"showSeriesPrevCloseValue\":false,\"showStudyLastValue\":false,\"showSymbolLabels\":false,\"showStudyPlotLabels\":false,\"showBidAskLabels\":false,\"showPrePostMarketPriceLabel\":true,\"showFundamentalNameLabel\":false,\"showFundamentalLastValue\":false,\"barSpacing\":6,\"showCurrency\":true,\"showUnit\":true},\"chartEventsSourceProperties\":{\"visible\":true,\"futureOnly\":true,\"breaks\":{\"color\":\"rgba(85, 85, 85, 1)\",\"visible\":false,\"style\":2,\"width\":1}},\"tradingProperties\":{\"showPositions\":true,\"positionPL\":{\"visibility\":true,\"display\":0},\"showOrders\":true,\"showExecutions\":true,\"horizontalAlignment\":0,\"extendLeft\":true,\"lineLength\":5,\"lineWidth\":1,\"lineStyle\":0},\"priceScaleSelectionStrategyName\":\"auto\"},\"lineToolsGroups\":{\"groups\":[]},\"version\":3,\"timezone\":\"Asia/Kolkata\",\"sessions\":{\"properties\":{\"graphics\":{\"backgrounds\":{\"outOfSession\":{\"color\":\"#2962FF\",\"transparency\":92,\"visible\":false},\"preMarket\":{\"color\":\"#FF9800\",\"transparency\":92,\"visible\":false},\"postMarket\":{\"color\":\"#2962FF\",\"transparency\":92,\"visible\":false}},\"vertlines\":{\"sessBreaks\":{\"color\":\"#4985e7\",\"style\":2,\"visible\":false,\"width\":1}}}}},\"shouldBeSavedEvenIfHidden\":false,\"chartId\":\"1\"}]}"}},
"symbol": "string",
"resolution": "string"
}
我尝试了以下baseModel
声明和async
fuction:
class Item(BaseModel):
name: str
content: dict
symbol: str
resolution: str
@app.post("/saveload/v1/charts")
async def create_item(item: Item, q: str | None = None):
return item
但这导致422无法取得的实体
错误,因为AS,您可以看到,dict
将JSON包裹在其卷发括号中,因此Fastapi正在抛出此错误。那么,如何声明basemodel
接收此数据?
A client app is sending data to a server using POST
method. The data basically have 4 key-value pairs out of which the value of the second key is a valid JSON object with multiple key-value pairs in it. It currently looks like this:
{
"name": "string",
"content": {{"resolution":"1","symbol_type":"stock","exchange":"NSE","listed_exchange":"","symbol":"NSE:SBIN-EQ","short_name":"SBIN","legs":"[{\"symbol\":\"SBIN\",\"pro_symbol\":\"SBIN\"}]","name":"asdwa","description":"","charts_symbols":"{\"1\":{\"symbol\":\"NSE:SBIN-EQ\"}}","is_realtime":"1","content":"{\"name\":\"asdwa\",\"layout\":\"s\",\"charts\":[{\"panes\":[{\"sources\":[{\"type\":\"MainSeries\",\"id\":\"RGTnHz\",\"state\":{\"style\":1,\"esdShowDividends\":true,\"esdShowSplits\":true,\"esdShowEarnings\":true,\"esdShowBreaks\":false,\"esdFlagSize\":2,\"showCountdown\":false,\"bidAsk\":{\"visible\":false,\"lineStyle\":1,\"lineWidth\":1,\"bidLineColor\":\"#2962FF\",\"askLineColor\":\"#EF5350\"},\"prePostMarket\":{\"visible\":true,\"lineStyle\":1,\"lineWidth\":1,\"preMarketColor\":\"#fb8c00\",\"postMarketColor\":\"#2962FF\"},\"highLowAvgPrice\":{\"highLowPriceLinesVisible\":false,\"highLowPriceLabelsVisible\":false,\"averageClosePriceLineVisible\":false,\"averageClosePriceLabelVisible\":false},\"showInDataWindow\":true,\"visible\":true,\"showPriceLine\":true,\"priceLineWidth\":1,\"priceLineColor\":\"\",\"baseLineColor\":\"#B2B5BE\",\"showPrevClosePriceLine\":false,\"prevClosePriceLineWidth\":1,\"prevClosePriceLineColor\":\"rgba( 85, 85, 85, 1)\",\"minTick\":\"default\",\"dividendsAdjustment\":{},\"sessionId\":\"regular\",\"sessVis\":false,\"statusViewStyle\":{\"fontSize\":16,\"showExchange\":true,\"showInterval\":true,\"symbolTextSource\":\"description\"},\"candleStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"drawWick\":true,\"drawBorder\":true,\"borderColor\":\"#378658\",\"borderUpColor\":\"#26a69a\",\"borderDownColor\":\"#ef5350\",\"wickColor\":\"#737375\",\"wickUpColor\":\"#26a69a\",\"wickDownColor\":\"#ef5350\",\"barColorsOnPrevClose\":false,\"drawBody\":true},\"hollowCandleStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"drawWick\":true,\"drawBorder\":true,\"borderColor\":\"rgba( 55, 134, 88, 1)\",\"borderUpColor\":\"#26a69a\",\"borderDownColor\":\"#ef5350\",\"wickColor\":\"rgba( 115, 115, 117, 1)\",\"wickUpColor\":\"#26a69a\",\"wickDownColor\":\"#ef5350\",\"drawBody\":true},\"haStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"drawWick\":true,\"drawBorder\":true,\"borderColor\":\"rgba( 55, 134, 88, 1)\",\"borderUpColor\":\"#26a69a\",\"borderDownColor\":\"#ef5350\",\"wickColor\":\"rgba( 115, 115, 117, 1)\",\"wickUpColor\":\"#26a69a\",\"wickDownColor\":\"#ef5350\",\"showRealLastPrice\":false,\"barColorsOnPrevClose\":false,\"inputs\":{},\"inputInfo\":{},\"drawBody\":true},\"barStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"barColorsOnPrevClose\":false,\"dontDrawOpen\":false,\"thinBars\":true},\"hiloStyle\":{\"color\":\"#2962FF\",\"showBorders\":true,\"borderColor\":\"#2962FF\",\"showLabels\":true,\"labelColor\":\"#2962FF\",\"fontSize\":7,\"drawBody\":true},\"lineStyle\":{\"color\":\"#2962FF\",\"linestyle\":0,\"linewidth\":2,\"priceSource\":\"close\",\"styleType\":2},\"areaStyle\":{\"color1\":\"rgba(41, 98, 255, 0.28)\",\"color2\":\"#2962FF\",\"linecolor\":\"#2962FF\",\"linestyle\":0,\"linewidth\":2,\"priceSource\":\"close\",\"transparency\":100},\"renkoStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"borderUpColor\":\"#26a69a\",\"borderDownColor\":\"#ef5350\",\"upColorProjection\":\"rgba( 169, 220, 195, 1)\",\"downColorProjection\":\"rgba( 245, 166, 174, 1)\",\"borderUpColorProjection\":\"rgba( 169, 220, 195, 1)\",\"borderDownColorProjection\":\"rgba( 245, 166, 174, 1)\",\"wickUpColor\":\"#26a69a\",\"wickDownColor\":\"#ef5350\",\"inputs\":{\"source\":\"close\",\"sources\":\"Close\",\"boxSize\":3,\"style\":\"ATR\",\"atrLength\":14,\"wicks\":true},\"inputInfo\":{\"source\":{\"name\":\"source\"},\"sources\":{\"name\":\"Source\"},\"boxSize\":{\"name\":\"Box size\"},\"style\":{\"name\":\"Style\"},\"atrLength\":{\"name\":\"ATR length\"},\"wicks\":{\"name\":\"Wicks\"}}},\"pbStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"borderUpColor\":\"#26a69a\",\"borderDownColor\":\"#ef5350\",\"upColorProjection\":\"rgba( 169, 220, 195, 1)\",\"downColorProjection\":\"rgba( 245, 166, 174, 1)\",\"borderUpColorProjection\":\"rgba( 169, 220, 195, 1)\",\"borderDownColorProjection\":\"rgba( 245, 166, 174, 1)\",\"inputs\":{\"source\":\"close\",\"lb\":3},\"inputInfo\":{\"source\":{\"name\":\"Source\"},\"lb\":{\"name\":\"Number of line\"}}},\"kagiStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"upColorProjection\":\"rgba( 169, 220, 195, 1)\",\"downColorProjection\":\"rgba( 245, 166, 174, 1)\",\"inputs\":{\"source\":\"close\",\"style\":\"ATR\",\"atrLength\":14,\"reversalAmount\":1},\"inputInfo\":{\"source\":{\"name\":\"Source\"},\"style\":{\"name\":\"Style\"},\"atrLength\":{\"name\":\"ATR length\"},\"reversalAmount\":{\"name\":\"Reversal amount\"}}},\"pnfStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"upColorProjection\":\"rgba( 169, 220, 195, 1)\",\"downColorProjection\":\"rgba( 245, 166, 174, 1)\",\"inputs\":{\"sources\":\"Close\",\"reversalAmount\":3,\"boxSize\":1,\"style\":\"ATR\",\"atrLength\":14,\"oneStepBackBuilding\":false},\"inputInfo\":{\"sources\":{\"name\":\"Source\"},\"boxSize\":{\"name\":\"Box size\"},\"reversalAmount\":{\"name\":\"Reversal amount\"},\"style\":{\"name\":\"Style\"},\"atrLength\":{\"name\":\"ATR length\"},\"oneStepBackBuilding\":{\"name\":\"One step back building\"}}},\"baselineStyle\":{\"baselineColor\":\"rgba( 117, 134, 150, 1)\",\"topFillColor1\":\"rgba( 38, 166, 154, 0.28)\",\"topFillColor2\":\"rgba( 38, 166, 154, 0.05)\",\"bottomFillColor1\":\"rgba( 239, 83, 80, 0.05)\",\"bottomFillColor2\":\"rgba( 239, 83, 80, 0.28)\",\"topLineColor\":\"rgba( 38, 166, 154, 1)\",\"bottomLineColor\":\"rgba( 239, 83, 80, 1)\",\"topLineWidth\":2,\"bottomLineWidth\":2,\"priceSource\":\"close\",\"transparency\":50,\"baseLevelPercentage\":50},\"rangeStyle\":{\"upColor\":\"#26a69a\",\"downColor\":\"#ef5350\",\"thinBars\":true,\"upColorProjection\":\"rgba( 169, 220, 195, 1)\",\"downColorProjection\":\"rgba( 245, 166, 174, 1)\",\"inputs\":{\"range\":10,\"phantomBars\":false},\"inputInfo\":{\"range\":{\"name\":\"Range\"},\"phantomBars\":{\"name\":\"Phantom bars\"}}},\"symbol\":\"NSE:SBIN-EQ\",\"shortName\":\"SBIN\",\"timeframe\":\"\",\"onWidget\":false,\"interval\":\"1\",\"unitId\":null,\"currencyId\":null,\"priceAxisProperties\":{\"autoScale\":true,\"autoScaleDisabled\":false,\"lockScale\":false,\"percentage\":false,\"percentageDisabled\":false,\"log\":false,\"logDisabled\":false,\"alignLabels\":true,\"isInverted\":false,\"indexedTo100\":false}},\"zorder\":0,\"haStyle\":{\"studyId\":\"BarSetHeikenAshi@tv-basicstudies-60\"},\"renkoStyle\":{\"studyId\":\"BarSetRenko@tv-prostudies-40\"},\"pbStyle\":{\"studyId\":\"BarSetPriceBreak@tv-prostudies-34\"},\"kagiStyle\":{\"studyId\":\"BarSetKagi@tv-prostudies-34\"},\"pnfStyle\":{\"studyId\":\"BarSetPnF@tv-prostudies-34\"},\"rangeStyle\":{\"studyId\":\"BarSetRange@tv-basicstudies-72\"}},{\"type\":\"study_Volume\",\"id\":\"bl7jkV\",\"state\":{\"styles\":{\"vol\":{\"linestyle\":0,\"linewidth\":1,\"plottype\":5,\"trackPrice\":false,\"transparency\":50,\"visible\":true,\"color\":\"#000080\",\"histogramBase\":0,\"joinPoints\":false,\"title\":\"Volume\"},\"vol_ma\":{\"linestyle\":0,\"linewidth\":1,\"plottype\":0,\"trackPrice\":false,\"transparency\":0,\"visible\":false,\"color\":\"#2196f3\",\"histogramBase\":0,\"joinPoints\":false,\"title\":\"Volume MA\"}},\"palettes\":{\"volumePalette\":{\"colors\":{\"0\":{\"color\":\"#EF5350\",\"width\":1,\"style\":0,\"name\":\"Falling\"},\"1\":{\"color\":\"#26A69A\",\"width\":1,\"style\":0,\"name\":\"Growing\"}}}},\"inputs\":{\"showMA\":false,\"length\":20,\"col_prev_close\":false},\"precision\":\"default\",\"bands\":{},\"area\":{},\"graphics\":{},\"showInDataWindow\":true,\"visible\":true,\"showStudyArguments\":true,\"intervalsVisibilities\":{\"ticks\":true,\"seconds\":true,\"secondsFrom\":1,\"secondsTo\":59,\"minutes\":true,\"minutesFrom\":1,\"minutesTo\":59,\"hours\":true,\"hoursFrom\":1,\"hoursTo\":24,\"days\":true,\"daysFrom\":1,\"daysTo\":366,\"weeks\":true,\"weeksFrom\":1,\"weeksTo\":52,\"months\":true,\"monthsFrom\":1,\"monthsTo\":12,\"ranges\":true},\"plots\":{\"0\":{\"id\":\"vol\",\"type\":\"line\"},\"1\":{\"id\":\"volumePalette\",\"palette\":\"volumePalette\",\"target\":\"vol\",\"type\":\"colorer\"},\"2\":{\"id\":\"vol_ma\",\"type\":\"line\"}},\"_metainfoVersion\":52,\"isTVScript\":false,\"isTVScriptStub\":false,\"is_hidden_study\":false,\"description\":\"Volume\",\"shortDescription\":\"Volume\",\"is_price_study\":false,\"id\":\"Volume@tv-basicstudies\",\"format\":{\"type\":\"volume\"},\"description_localized\":\"Volume\",\"shortId\":\"Volume\",\"packageId\":\"tv-basicstudies\",\"version\":\"1\",\"fullId\":\"Volume@tv-basicstudies-1\",\"productId\":\"tv-basicstudies\",\"name\":\"Volume@tv-basicstudies\",\"_serverMetaInfoVersion\":52},\"zorder\":-10000,\"ownFirstValue\":null,\"metaInfo\":{\"palettes\":{\"volumePalette\":{\"colors\":{\"0\":{\"name\":\"Falling\"},\"1\":{\"name\":\"Growing\"}}}},\"inputs\":[{\"id\":\"showMA\",\"name\":\"show MA\",\"defval\":false,\"type\":\"bool\",\"isHidden\":true},{\"id\":\"length\",\"name\":\"MA Length\",\"defval\":20,\"type\":\"integer\",\"min\":1,\"max\":2000},{\"defval\":false,\"id\":\"col_prev_close\",\"name\":\"Color based on previous close\",\"type\":\"bool\"}],\"plots\":[{\"id\":\"vol\",\"type\":\"line\"},{\"id\":\"volumePalette\",\"palette\":\"volumePalette\",\"target\":\"vol\",\"type\":\"colorer\"},{\"id\":\"vol_ma\",\"type\":\"line\"}],\"graphics\":{},\"defaults\":{\"styles\":{\"vol\":{\"linestyle\":0,\"linewidth\":1,\"plottype\":5,\"trackPrice\":false,\"transparency\":50,\"visible\":true,\"color\":\"#000080\"},\"vol_ma\":{\"linestyle\":0,\"linewidth\":1,\"plottype\":0,\"trackPrice\":false,\"transparency\":0,\"visible\":false,\"color\":\"#2196F3\"}},\"palettes\":{\"volumePalette\":{\"colors\":{\"0\":{\"color\":\"#EF5350\",\"width\":1,\"style\":0},\"1\":{\"color\":\"#26A69A\",\"width\":1,\"style\":0}}}},\"inputs\":{\"showMA\":false,\"length\":20,\"col_prev_close\":false}},\"_metainfoVersion\":52,\"isTVScript\":false,\"isTVScriptStub\":false,\"is_hidden_study\":false,\"styles\":{\"vol\":{\"title\":\"Volume\",\"histogramBase\":0},\"vol_ma\":{\"title\":\"Volume MA\",\"histogramBase\":0}},\"description\":\"Volume\",\"shortDescription\":\"Volume\",\"is_price_study\":false,\"id\":\"Volume@tv-basicstudies-1\",\"format\":{\"type\":\"volume\"},\"description_localized\":\"Volume\",\"shortId\":\"Volume\",\"packageId\":\"tv-basicstudies\",\"version\":\"1\",\"fullId\":\"Volume@tv-basicstudies-1\",\"productId\":\"tv-basicstudies\",\"name\":\"Volume@tv-basicstudies\",\"_serverMetaInfoVersion\":52}}],\"leftAxisesState\":[],\"rightAxisesState\":[{\"state\":{\"id\":\"AEh8VAL7anFf\",\"m_priceRange\":{\"m_maxValue\":505.6,\"m_minValue\":494},\"m_isAutoScale\":true,\"m_isPercentage\":false,\"m_isIndexedTo100\":false,\"m_isLog\":false,\"m_isLockScale\":false,\"m_isInverted\":false,\"m_height\":641.6,\"m_topMargin\":0.1,\"m_bottomMargin\":0.08,\"alignLabels\":true},\"sources\":[\"RGTnHz\"]}],\"overlayPriceScales\":{\"bl7jkV\":{\"id\":\"aYA6FWoeiTkP\",\"m_priceRange\":{\"m_maxValue\":298353,\"m_minValue\":0},\"m_isAutoScale\":true,\"m_isPercentage\":false,\"m_isIndexedTo100\":false,\"m_isLog\":false,\"m_isLockScale\":false,\"m_isInverted\":false,\"m_height\":641.6,\"m_topMargin\":0.1,\"m_bottomMargin\":0.08,\"alignLabels\":true}},\"stretchFactor\":2000,\"mainSourceId\":\"RGTnHz\",\"priceScaleRatio\":null}],\"timeScale\":{\"m_barSpacing\":6,\"m_rightOffset\":10},\"chartProperties\":{\"paneProperties\":{\"backgroundType\":\"solid\",\"background\":\"#ffffff\",\"backgroundGradientStartColor\":\"#ffffff\",\"backgroundGradientEndColor\":\"#ffffff\",\"vertGridProperties\":{\"color\":\"rgba(42, 46, 57, 0.06)\",\"style\":0},\"horzGridProperties\":{\"color\":\"rgba(42, 46, 57, 0.06)\",\"style\":0},\"crossHairProperties\":{\"color\":\"#9598A1\",\"style\":2,\"transparency\":0,\"width\":1},\"topMargin\":10,\"bottomMargin\":8,\"axisProperties\":{\"autoScale\":true,\"autoScaleDisabled\":false,\"lockScale\":false,\"percentage\":false,\"percentageDisabled\":false,\"indexedTo100\":false,\"log\":false,\"logDisabled\":false,\"alignLabels\":true,\"isInverted\":false},\"legendProperties\":{\"showStudyArguments\":true,\"showStudyTitles\":true,\"showStudyValues\":true,\"showSeriesTitle\":true,\"showSeriesOHLC\":true,\"showLegend\":true,\"showBarChange\":true,\"showBackground\":true,\"backgroundTransparency\":50,\"wrapText\":false}},\"scalesProperties\":{\"backgroundColor\":\"#ffffff\",\"lineColor\":\"rgba(42, 46, 57, 0.14)\",\"textColor\":\"#131722\",\"fontSize\":12,\"scaleSeriesOnly\":false,\"showSeriesLastValue\":true,\"seriesLastValueMode\":1,\"showSeriesPrevCloseValue\":false,\"showStudyLastValue\":false,\"showSymbolLabels\":false,\"showStudyPlotLabels\":false,\"showBidAskLabels\":false,\"showPrePostMarketPriceLabel\":true,\"showFundamentalNameLabel\":false,\"showFundamentalLastValue\":false,\"barSpacing\":6,\"showCurrency\":true,\"showUnit\":true},\"chartEventsSourceProperties\":{\"visible\":true,\"futureOnly\":true,\"breaks\":{\"color\":\"rgba(85, 85, 85, 1)\",\"visible\":false,\"style\":2,\"width\":1}},\"tradingProperties\":{\"showPositions\":true,\"positionPL\":{\"visibility\":true,\"display\":0},\"showOrders\":true,\"showExecutions\":true,\"horizontalAlignment\":0,\"extendLeft\":true,\"lineLength\":5,\"lineWidth\":1,\"lineStyle\":0},\"priceScaleSelectionStrategyName\":\"auto\"},\"lineToolsGroups\":{\"groups\":[]},\"version\":3,\"timezone\":\"Asia/Kolkata\",\"sessions\":{\"properties\":{\"graphics\":{\"backgrounds\":{\"outOfSession\":{\"color\":\"#2962FF\",\"transparency\":92,\"visible\":false},\"preMarket\":{\"color\":\"#FF9800\",\"transparency\":92,\"visible\":false},\"postMarket\":{\"color\":\"#2962FF\",\"transparency\":92,\"visible\":false}},\"vertlines\":{\"sessBreaks\":{\"color\":\"#4985e7\",\"style\":2,\"visible\":false,\"width\":1}}}}},\"shouldBeSavedEvenIfHidden\":false,\"chartId\":\"1\"}]}"}},
"symbol": "string",
"resolution": "string"
}
I tried the following BaseModel
declaration and an async
fuction:
class Item(BaseModel):
name: str
content: dict
symbol: str
resolution: str
@app.post("/saveload/v1/charts")
async def create_item(item: Item, q: str | None = None):
return item
but this leads to 422 Unprocessable Entity
error, because, as you can see, the dict
wraps JSON in its curly braces due to which FastAPI is throwing this error. So, how should I declare the BaseModel
to receive this data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢 @
Matslindh和 @bijayregmi,我搜索了他们的建议并找到了解决方案,只需在从最终
JSON
对象构建Pydanticbasemodel
。一切都会好起来的。Thanks to @
MatsLindh and @BijayRegmi,I googled their suggestions and found the solution, just use utility on https://jsontopydantic.com/ to build pydantic
BaseModel
from your finaljson
object . And everything will work fine.如上面的注释部分所述,
{{
...}}
是不是有效的JSON类型。您应该用Square Brackets,即,[{...}]
替换示例中content
的值的外部卷曲括号。例如:完整输入示例:
因此,相应的pydantic模型应如下:
我强烈建议您查看 A>有关
422无法处理的实体
错误的更多详细信息,以及各种方法给出的示例,可用于定义FastAPI端点期望JSON数据。As mentioned already in the comments section above,
{{
...}}
is not a valid JSON type. You should rather replace the outer curly brackets of thecontent
's value in your example with square brackets, i.e.,[{ ... }]
. For instance:Complete input example:
Hence, the corresponding Pydantic model should be as follows:
I would highly recommend having a look at this answer for further details on
422 Unprocessable entity
errors, and the examples given on the various methods that could be used to define a FastAPI endpoint expecting JSON data.