You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working on a FastAPI project where I'm trying to use the HashModel class from the redis_om library to interact with a Redis database. However, I've encountered an issue related to Pydantic when defining a response model for one of my API endpoints. Here's my code:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from redis_om import get_redis_connection, HashModel
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=['http://localhost:3000'],
allow_methods=['*'],
allow_headers=['*'])
redis = get_redis_connection(
host='redis-hos.cloud.redislabs.com',
port='port',
password='password',
decode_responses=True)
class Product(HashModel):
name: str
price: float
quantity: int
class Meta:
database = redis
@app.get("/")
async def root():
return {"message": "Hello, World!"}
@app.get("/product")
def all():
# Fetch and return all products
products = Product.all()
return products
@app.post("/addproduct")
async def create(product: Product):
# Create a new Product instance and save it to Redis
return product.save()
This is the error :
fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'main.Product'> is a valid Pydantic field type. If you are using a return type annotation that is not a valid Pydantic field (e.g. Union[Response, dict, None]) you can disable generating the response model from the type annotation with the path operation decorator parameter response_model=None. Read more: https://fastapi.tiangolo.com/tutorial/response-model/
The text was updated successfully, but these errors were encountered:
+1 - Facing same issue but with JsonModel derived class.
from typing import List
from aredis_om.model import JsonModel, Field
class Population(JsonModel):
description: str
session_ids: List[str] = Field(description="A list of session_ids associated with this population", default=[])
...
@population.get("/{population_id}")
async def get_population(population_id: str) -> Population:
"""
Get a specific populations
"""
...
Just starting this code gives: fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'project.api.models.Population'> is a valid Pydantic field type. If you are using a return type annotation that is not a valid Pydantic field (e.g. Union[Response, dict, None]) you can disable generating the response model from the type annotation with the path operation decorator parameter response_model=None. Read more: https://fastapi.tiangolo.com/tutorial/response-model/
I'm working on a FastAPI project where I'm trying to use the HashModel class from the redis_om library to interact with a Redis database. However, I've encountered an issue related to Pydantic when defining a response model for one of my API endpoints. Here's my code:
This is the error :
fastapi.exceptions.FastAPIError: Invalid args for response field! Hint: check that <class 'main.Product'> is a valid Pydantic field type. If you are using a return type annotation that is not a valid Pydantic field (e.g. Union[Response, dict, None]) you can disable generating the response model from the type annotation with the path operation decorator parameter response_model=None. Read more: https://fastapi.tiangolo.com/tutorial/response-model/
The text was updated successfully, but these errors were encountered: