I am using flask-sqlalchemy and have a model representing a fish, which has a birthday as a Date object. Associated with the Fish class is a method which returns the number of months the fish has been alive, is there a way to filter based on what is returned from that custom method?
Here is my code
#the model
class Fish(db.Model):
id = db.Column(db.Integer, primary_key=True)
...
birthday = db.Column(db.Date)
#the method for returning number of months
def getMonths(self):
if self.status == "Dead":
return
if self.birthday == None:
return
today = datetime.today().date()
birthday = self.birthday
age_difference = relativedelta.relativedelta(today,birthday)
return age_difference.months
Here is what I have tried so far
#number or months to filter by
cutoff = 6
#the query
result = Fish.query.filter(Fish.getMonths() >= cutoff).all()
but this is returning a type error, because there is no self passed to the method
TypeError: getMonths() missing 1 required positional argument: 'self'
Any help on how to do this would be greatly appreciated and let me know if you need any more information!