我正在尝试使用 marshmallow_sqlalchemy 创建一个模式。我想从数据库的两个条目 x_coordinate 和 y_coordinate 创建一个列表,但是,我不知道该怎么做。
这是架构
from marshmallow_sqlalchemy import SQLAlchemySchema, auto_field
class LocationSQLAlchemySchema(SQLAlchemySchema):
class Meta:
model = Location
load_instance = True
location_id = auto_field('id')
name = auto_field('name')
user_uid = auto_field('customer_id')
maps_url = auto_field('maps_url')
coordinates = fields.List(Meta.model.x_coordinate,Meta.model.y_coordinate) #This is what I don't know how to define.
这是我用于此架构的模型:
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.dialects.mysql import INTEGER
db = SQLAlchemy()
class Location(db.Model):
__tablename__ = 'cleaning_locations'
id = db.Column(db.String(128), primary_key=True, unique = True)
name = db.Column(db.String(45), nullable = False)
customer_id = db.Column(db.String(45),nullable = False)
maps_url = db.Column(db.String(2048),nullable = False)
x_coordinate = db.Column(db.Numeric(precision = 8, scale=5), nullable = False)
y_coordinate = db.Column(db.Numeric(precision = 8, scale=5), nullable = False)
address = db.Column(db.String(255))
country = db.Column(db.String(45))
state = db.Column(db.String(45))
size = db.Column(INTEGER(unsigned=True), nullable = False )
rooms = db.Column(INTEGER(unsigned=True), nullable = False )
bathrooms = db.Column(INTEGER(unsigned=True), nullable = False )
kitchens = db.Column(INTEGER(unsigned=True), nullable = False )
- 如何使架构将坐标表示为包含 x_coordinate 和 y_coordinate 的列表?
- (新手额外问题)。在 db 模型和 Schema 中使用不同的变量名是一种不好的做法吗?