0

I have a SQLAlchemy model that looks something like this:

class Guest(db.Model):
    name = db.Column(db.String)

    current_status_id = db.Column(db.Integer, db.ForeignKey('approval_status.id'))
    current_status = db.relationship('ApprovalStatus')

I would like to have a method like this:

    def reset_status(self):
        awaiting_response = db.session.query(Approvalstatus).get(1)
        self.current_status = awaiting_response

Querying from within the model does not seem right to me for some reason. However, it seems that this logic belongs on the Guest class.

Currently, I have a guest service that looks something like this:

def reset_guest_approval(guest):
        awaiting_response = db.session.query(Approvalstatus).get(1)
        guest.current_status = awaiting_response

I want to consolidate the logic into the class if possible. What are the best practices on this?

1 Answer 1

1

In my similar case, I used object_session for making a query inside the class function.

class Guest(db.Model):
    name = db.Column(db.String)

    current_status_id = db.Column(db.Integer, db.ForeignKey('approval_status.id'))
    current_status = db.relationship('ApprovalStatus')

    def reset_status(self):
        awaiting_response = object_session(self).query(Approvalstatus).get(1)
        self.current_status = awaiting_response
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.