我有几个看起来像这样的简单模型:
class StoreImage(Base):
imagepath = Column(Text, nullable=False)
store_id = Column(Integer, ForeignKey('store.id'), nullable=False)
store = relationship('Store')
class Store(Base):
status = Column(Enum('published', 'verify', 'no_position',
name='store_status'),
nullable=False, default='verify')
type = Column(Enum('physical', 'web', name='store_type'),
nullable=False, default='physical')
name = Column(Text, nullable=False)
street = Column(Text)
postal_code = Column(Text)
city = Column(Text)
country = Column(Text)
phone_number = Column(Text)
email = Column(Text)
website = Column(Text)
location = Column(Geometry(geometry_type='POINT', srid=4326))
is_flagship = Column(Boolean, default=False)
images = relationship(StoreImage)
现在我想做的是这样的查询:
q = Store.query.filter(Store.is_flagship == True).with_entities(
Store.id,
Store.status,
Store.slug,
Store.type,
Store.name,
Store.street,
Store.postal_code,
Store.city,
Store.country,
Store.phone_number,
Store.email,
Store.website,
func.ST_AsGeoJSON(Store.location).label('location'),
Store.images,
)
查询有效,但Store.images
只返回True
每一行。如何让它返回StoreImage
实例/KeyedTuples 列表?
我想这样做主要是因为我还没有找到任何其他方法来以Store.query
GeoJSON 格式返回位置。
编辑:对我来说,一种解决方案是从查询中返回Store
实例并以某种方式添加location
GeoJSON,无论是在声明的模型上还是在可能的其他方式上。虽然不知道该怎么做。