'Run filter on sqlalchemy Model relationship before passing to Marshmellow

I have two sqlalchemy models with a one-to-many relationship using lazy="dynamic like so:

class ProjectModel(db.Model):
    __tablename__ = "projects"
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(1000), nullable=False, unique=True)
    jobs = db.relationship("JobModel", backref="project", cascade="all,delete", lazy="dynamic")
class JobModel(db.Model):
    id = db.Column(db.Integer, primary_key=True)

When I deserialize a project to serve on my viewhandler, I want to run a filter on the jobs, something like so (which results in an error):

project = db.session.query(ProjectModel.first())
project.jobs = project.jobs.filter(JobModel.id == "36")

project_dump = ProjectSchema().dump(project)

The filter itself works fine, but I can't replace the jobs key in the Model object, is there another way I can run this filter so it applies when passed into a Marshmellow schema?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source