Reputation: 848
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?
Upvotes: 0
Views: 236
Reputation: 585
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
Upvotes: 1