我和吉诺陷入了麻烦。我可以从我的表中获得一个称为模板的行,所以在另一条路径中,我希望从表中获取所有行。在这里,你可以看到我的观点,从分贝蚂蚁得到一个记录,它工作得很完美。
@router.get("/templates/{uid}")
async def get_template(uid: str):
temp = await Template.get_or_404(uid)
return temp.to_dict()接下来,您可以查看我的视图将记录添加到db中,并且它也运行良好:
@router.post("/templates")
async def add_template(template: TemplateModel):
rv = await Template.create(name=template.name, text=template.text)
return rv.to_dict()所以,在这个观点中,主要原因是它不起作用:
@router.get("/templates/all")
async def get_all_templates():
temp = await Template.all()
return temp.to_dict()下面向下看我的模板模型:
class Template(db.Model):
__tablename__ = "templates"
UUID = db.Column(
str(UUID(as_uuid=True)),
db.String,
primary_key=True,
default=str(uuid.uuid4()),
unique=True,
nullable=False,
)
name = db.Column("name", db.String, nullable=False)
text = db.Column("text", db.String, nullable=False)
created_at = db.Column("created_at", db.DateTime, nullable=False,
server_default=db.func.now())最后是我的db GINO引擎:
from gino_starlette import Gino
from .. import config
db = Gino(
dsn=config.DB_DSN,
pool_min_size=config.DB_POOL_MIN_SIZE,
pool_max_size=config.DB_POOL_MAX_SIZE,
echo=config.DB_ECHO,
)错误日志:
2020-08-08 12:07:57,698 INFO gino.engine._SAEngine SELECT templates."UUID", templates.name, templates.text, templates.created_at
FROM templates
WHERE templates."UUID" = $1
2020-08-08 12:07:57,699 INFO gino.engine._SAEngine ('all',)
INFO: 172.23.0.1:40676 - "GET /templates/all HTTP/1.1" 404 Not Found请告诉我怎么了,我花了那么多时间来解决这个问题。不用客气,谢谢。
发布于 2020-08-11 11:50:27
不过我不知道怎么回事。而GINO文档并没有告诉使用使用GINO模型的db的操作(如从db获取所有记录)应该在POST中请求。所以正确的答案是将router.get替换为router.post:
@router.post("/templates/all")
async def get_all_templates():
temp = await Template.all()
return temp.to_dict()https://stackoverflow.com/questions/63315405
复制相似问题