标签云

微信群

扫码加入我们

WeChat QR Code

Hello everyone : I have struggle with this for some time now.I have a Model as under:class Job(db.Model):__tablename__ = "job"id = db.Column(db.Integer, primary_key=True)title = db.Column(db.String(80), nullable=False)city = db.Column(db.String(250), nullable=False)state = db.Column(db.String(10), nullable=False)zipCode = db.Column(db.String(10), nullable=False)latitude = db.Column(db.String(10), nullable=False)longitude = db.Column(db.String(10), nullable=False)description = db.Column(db.String(200), nullable=False)narrative = db.Column(db.String(250), nullable=False)companyLogo = db.Column(db.String(250), nullable=False)companyName = db.Column(db.String(250), nullable=False)companyURL = db.Column(db.String(100), nullable=False)time_created = db.Column(db.DateTime, nullable=False)active = db.Column(db.Boolean, nullable=False)I have a @classmethod that uses a Postgres query and in it I want to return the Job and a calculated distance . This work perfectly.Here is the relevant part of the class method : # defining an statement with a subquery that use our hybrid methodstmt = db.session.query(Job,Job.distance(lat1, lon1).label('distance')).\subquery()# defining and aliases in the table to use above statementjob_alias = aliased(Job, stmt)jobs = db.session.query(job_alias).\filter(stmt.c.distance < dst).\order_by(stmt.c.distance).\paginate(page, per_page)return jobsThis works perfectly. Each Job in Jobs indeed contains the column distanceHowever when the serialization(dump) process starts in my Marshmallow Schema , I do not know what to do to return distance to the view, as distance is not part of the Model How do we handle this in Marshmallow?here is all that I have in my Schema for this tableclass JobSchema(ma.ModelSchema):def validate_state(state):"""Code removed for clarity"""def validate_zipCode(zip): """Code removed for clarity"""@pre_loaddef get_longitude_for_zip(self, data): """Code removed for clarity"""title = fields.Str(required=True, validate=[validate.Length(min=4, max=80)])city = fields.Str(required=True, validate=[validate.Length(min=5, max=100)])state = fields.Str(required=True, validate=validate_state)zipCode = fields.Str(required=True, validate=validate_zipCode)description = fields.Str(required=False, validate=[validate.Length(max=250)])narrative = fields.Str(required=False, validate=[validate.Length(max=250)])companyLogo = fields.Str(required=False, validate=[validate.Length(max=250)])companyName = fields.Str(required=True, validate=[validate.Length(min=5, max=250)])companyURL = fields.Str(required=True, validate=[validate.Length(min=4, max=100)])latitude = fields.Str(required=True)longitude = fields.Str(required=True)time_created = fields.DateTime()active = fields.Boolean(required=True)class Meta:model = modelJobCould someone help?Thanks.