redis / redis-om-python

Object mapping, and more, for Redis and Python
MIT License
1.12k stars 112 forks source link

How should I use the .find() method with FastAPI? #328

Closed munhyok closed 2 years ago

munhyok commented 2 years ago
class Form(JsonModel):
    name: str = Field(index=True)
    company: str = Field(index=True)
    #price: int
    date: str = Field(index=True, full_text_search=True)
    description: str = Field(index=True)
    platform: str = Field(index=True)
    g_url: str = Field(index=True)
    i_url: str = Field(index=True)

    screenshots: List[str] = Field(index=True)

    class Meta:
        database = redis

After creating a JsonModel called Form as above I want to get only JSON that contains a specific date using the .find() method. So I wrote the code below.

@app.get('/games/{date_}')
def get_game_path(date_: str):
    formgame = Form.find(Form.date == date_).all

    return formgame

But when I write like that, FastAPI outputs redis_om.model.model.NotFoundError

https://www.youtube.com/watch?v=Cy9fAvsXGZA&list=LL&index=2&t=1208s The YouTube tutorial is output when you input a specific pk value into the address bar, but can't it be done with date?

wiseaidev commented 2 years ago

As the error message suggests, you might forget to run migrations first as the readme file says; So try something like:

from fastapi import FastAPI

from redis_om import (
    Migrator
)

app = FastAPI(title=__name__)

@app.on_event("startup")
async def startup():
    await Migrator().run()

And don't forget to call the all method: all()