Hello,
I have a problem regarding redis_om in python. I want to work with FastAPI and Redis OM. I have the following schemas in schemas.py:
from pydantic import EmailStr
from typing import List, Optional
from redis_om import (
HashModel,
EmbeddedJsonModel,
JsonModel,
Field,
Migrator,
)
class Stock(EmbeddedJsonModel):
name: str = Field(index=True)
class Portfolio(JsonModel):
stocks: Stock
Each Portfolio consist of one stock. Thats why stock is an EmbeddedJsonModel, while Portfolio is a JsonModel.
I have the following FastAPI code in app.py:
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from starlette.requests import Request
from starlette.responses import Response
from redis_om import NotFoundError
from redis_om import get_redis_connection
from pydantic import ValidationError
from core.schemas.schemas import Portfolio
# This Redis instance is tuned for durability.
REDIS_DATA_URL = "redis://localhost:6380"
# This Redis instance is tuned for cache performance.
REDIS_CACHE_URL = "redis://localhost:6381"
app = FastAPI()
@app.get("/", response_class=HTMLResponse)
async def root():
return '''
<a href='http://127.0.0.1:8000/docs'>http://127.0.0.1:8000/docs</a>
'''
@app.post("/portfolio")
async def save_portfolio(portfolio: Portfolio):
return portfolio.save()
Now when i start the FastAPI app and do a save_portfolio request from the docs like this:
{
"pk": "string",
"stocks": {
"pk": "string",
"name": "string"
}
}
With the corresponding curl:
curl -X 'POST' \
'http://127.0.0.1:8000/portfolio' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"pk": "string",
"stocks": {
"pk": "string",
"name": "string"
}
}'
It says
127.0.0.1:51640 - "POST /portfolio HTTP/1.1" 422 Unprocessable Entity
and
'{"detail":[{"loc":["body"],"msg":"\'NoneType\' object is not subscriptable","type":"type_error"}]}'
In my view the request agrees with the pydantic model.
Any help will be deeply appreciated.