当我尝试运行 FastApi api 时,出现此错误。
app = cls(app=app, **options) TypeError: 'module' 对象不可调用
我正在尝试在与 main.py 分开的其他文件夹中添加中间件,但不知道为什么不起作用。否则,当我将中间件代码添加到 main.py 中时,工作不会出现问题。这是我的代码,感谢您的帮助,请原谅我的英语。
主要.py
from fastapi import FastAPI
from fastapi import Depends, FastAPI, HTTPException
from fastapi import Request
from routers import rutas
from utils import CheckApiKey
from utils.CheckApiKey import check_api_key
app = FastAPI()
app.add_middleware(CheckApiKey, dispatch=check_api_key) <--- Here calling middleware
app.include_router(rutas.router)
if __name__ == "__main__":
import uvicorn
uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)
Run Code Online (Sandbox Code Playgroud)
中间件
from fastapi import Request
async def check_api_key(request: Request, call_next):
print("ok")
response = await call_next(request)
return response
Run Code Online (Sandbox Code Playgroud) 我有这样的结构:
SqlAlchemy 模型
class MPrueba(Base):
__tablename__ = 'M_pruebas'
idpruebas = Column(Integer, primary_key=True)
idfuentes = Column(ForeignKey('M_fuentes.idfuentes', ondelete='RESTRICT', onupdate='RESTRICT'), index=True)
M_fuente = relationship('MFuente')
class MRegla(Base):
__tablename__ = 'M_reglas'
idreglas = Column(Integer, primary_key=True)
idpruebas = Column(ForeignKey('M_pruebas.idpruebas', ondelete='RESTRICT', onupdate='RESTRICT'), index=True)
nombre = Column(String(40))
M_prueba = relationship('MPrueba')
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,MRegla 类上存在指向 MPrueba 类的关系。这意味着当我对 MRegla 类发出一些 get 请求时,M_prueba 字段应包含来自 MPrueba 类的数据。我如何从 MPrueba 类访问该关系?我想生成一个像这样的 pydantic 模型:
MPrueba 类的 pydantic 模式
class Prueba(BaseModel):
idpruebas: int
idfuentes: int
reglas : # Append the MRegla here
class Config:
orm_mode = True
Run Code Online (Sandbox Code Playgroud)
感谢您的帮助。
我最近正在使用 fastapi,作为练习,我想将我的 fastapi API 与其他服务器上的验证服务连接...但我不知道如何做到这一点,我还没有在官方文档中找到对我有帮助的东西..我必须用python代码来做吗?或者有什么办法吗?
当我尝试从 postgre 数据库获取一些数据并使用 fastapi 时,出现此错误。
我不知道为什么会发生这种情况...但这是我的代码,谢谢您的帮助。
路线
@router.get("/fuentes", response_model=FuenteSerializer.MFuente) # <--- WHEN I REMOVE RESPONSE_MODEL WORKS AND RETURNS A JSON DATA DIRECTLY FROM MODEL I GUESS
async def read_fuentes(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
fuentes = FuenteSerializer.get_fuente(db, skip=skip, limit=limit)
return fuentes
Run Code Online (Sandbox Code Playgroud)
sqlalchemy 模型
class MFuente(Base):
__tablename__ = 'M_fuentes'
idfuentes = Column(Integer, primary_key=True)
idproductos = Column(ForeignKey('M_productos.idproductos', ondelete='RESTRICT', onupdate='RESTRICT'), index=True)
autoapp = Column(CHAR(2))
rutFabricante = Column(String(12))
elemento = Column(String(100))
estado = Column(Integer)
stype = Column(Integer)
aql …Run Code Online (Sandbox Code Playgroud) 我正在学习在 postgresql 中使用 fastapi。我有一个来自其他项目的巨大数据库,它已经作为 postgre 数据库完成了……所以我尝试添加 fastapi 但不知道我该怎么做……看来您必须使用 fastapi 正确创建所有数据库模式从一开始......我找不到任何有用的例子,所以我希望你能指导我。谢谢你的时间,请原谅我的英语
我有一个 fastapi 应用程序,我想记录在它上面发出的每个请求。我正在尝试为此使用 loguru 和 uvicorn,但我不知道如何打印与每个请求关联的标头和请求参数(如果有)。
我想要这样的东西:
INFO 2020-08-13 13:36:33.494 uvicorn.protocols.http.h11_impl:send - 127.0.0.1:52660 - "GET
/url1/url2/ HTTP/1.1" 400 params={"some": value, "some1":value}
Run Code Online (Sandbox Code Playgroud)
有办法吗?谢谢你的帮助。
这里有一些链接:
fastapi ×6
python ×6
api ×3
pydantic ×2
rest ×2
sqlalchemy ×2
logging ×1
middleware ×1
postgresql ×1
restapi ×1
uvicorn ×1