我刚刚开始使用 FastAPI、SQLAlchemy、Pydantic,我正在尝试构建一个简单的 API 端点,以将 postgis 表中的行作为 geojson 特征集合返回。
这是我的sqlalchemy模型:
class Poi(Base):
__tablename__ = 'poi'
id = Column(Integer, primary_key=True)
name = Column(Text, nullable=False)
type_id = Column(Integer)
geometry = Column(Geometry('POINT', 4326, from_text='ST_GeomFromEWKT'),
nullable=False)
Run Code Online (Sandbox Code Playgroud)
使用的geojson_pydantic相关pydantic型号有:
from geojson_pydantic.features import Feature, FeatureCollection
from geojson_pydantic.geometries import Point
from typing import List
class PoiProperties(BaseModel):
name: str
type_id: int
class PoiFeature(Feature):
id: int
geometry: Point
properties: PoiProperties
class PoiCollection(FeatureCollection):
features: List[PoiFeature]
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望能够检索并返回数据库记录,如下所示:
def get_pois(db: Session, skip: int = 0, limit: int …Run Code Online (Sandbox Code Playgroud)