The only difference between my code and yours is that I am not using sessions. Might it be the reason why my query is not working as expected? Here is my query one more time: monthly_events = current_user.followed_events().filter(Event.event_date < datetime.today().date()).filter(func.strftime('%m', Event.event_date) == datetime.today().strftime('%m')).order_by(Event.timestamp.desc()) Again, this is the problem the query is trying to solve. I want to know the subscribed events (events that I am following) that took place in the current month. This is irrespective of whether they took place 5 years ago, or a year ago, so long as they took place in the current month.
However, the query gives no events that took place in the current month, yet I have events that indeed took place in April (the current month). The current month is to be automated, which I have successfully accomplished through *datetime.today().strftime('%m')*. By breaking the query into sections, I was able to know where the problem is. The section of the query: *current_user.followed_events().filter(Event.event_date < datetime.today().date())*gives all events that have passed (yesterday and beyond). This part works correctly. The section: *current_user.followed_events().filter(Event.event_date < datetime.today().date()).order_by(Event.timestamp.desc())*arranges these past events in descending order and this section works correctly, as well. However, the part with problem is: .filter(func.strftime('%m', Event.event_date) == datetime.today().strftime('%m'))where the aim is to filter out events that took place in the current month, irrespective of the year they took place. Note that I have imported the following modules from sqlalchemy import func and from datetime import datetimeat the top of the routes.py. The *event_date* field in the *models.py* is stored as a *db.DateTime* with a *default = datetime.utcnow*. I am using Flask, with *SQLite* db, but will change it to *Postgresql* later. I hope the information is enough, otherwise let me know if additional information is needed. On Wed, Apr 5, 2023 at 10:21 AM James Paul Chibole <jpchib...@gmail.com> wrote: > Sorry, it is a type, it should actually be .filter(func.strftime('%m', > Event.event_date == datetime.today().strftime('%m'))) > > Let me go through your latest response and will get back to you. Thank you > for the prompt response. > > On Wed, Apr 5, 2023 at 10:16 AM Lele Gaifax <l...@metapensiero.it> wrote: > >> Nancy Andeyo <nandey...@gmail.com> writes: >> >> > However, the part with problems is this one: .filter(func.strftime('%m', >> > Event.event_date = datetime.today().strftime('%m'))) where the aim to >> > filter out events that will take place in the current month. This is the >> > section that I posted, yet I needed to post the entire query for what I >> am >> > intending to achieve is understood. >> >> I can't say if the typos are due to you rewriting the cide in these >> messages, or if instead they are effectively present in the real code, >> but also the above is not correct: >> >> .filter(func.strftime('%m', Event.event_date = >> datetime.today().strftime('%m'))) >> >> This should raise a syntax error when evaluated by Python... >> >> For comparison, the following complete script works for me: >> >> from datetime import date >> from pprint import pprint >> >> from sqlalchemy import create_engine >> from sqlalchemy import func >> from sqlalchemy.orm import Session >> >> from sol.models import Tourney >> >> >> engine = create_engine('sqlite:///development.db') >> session = Session(engine) >> >> q = session.query(Tourney) >> q = q.filter(func.strftime('%m', Tourney.date) == >> date.today().strftime('%m')) >> >> pprint([(t.description, t.date) for t in q.limit(3).all()]) >> >> and emits >> >> [('2° Torneo', datetime.date(2001, 4, 1)), >> ('7° Torneo', datetime.date(2004, 4, 24)), >> ('7° Torneo', datetime.date(2005, 4, 30))] >> >> Hope this helps, >> ciao, lele. >> -- >> nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri >> real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. >> l...@metapensiero.it | -- Fortunato Depero, 1929. >> >> -- >> SQLAlchemy - >> The Python SQL Toolkit and Object Relational Mapper >> >> http://www.sqlalchemy.org/ >> >> To post example code, please provide an MCVE: Minimal, Complete, and >> Verifiable Example. See http://stackoverflow.com/help/mcve for a full >> description. >> --- >> You received this message because you are subscribed to a topic in the >> Google Groups "sqlalchemy" group. >> To unsubscribe from this topic, visit >> https://groups.google.com/d/topic/sqlalchemy/4oPfuzAjw48/unsubscribe. >> To unsubscribe from this group and all its topics, send an email to >> sqlalchemy+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/sqlalchemy/87pm8ier3i.fsf%40metapensiero.it >> . >> > > > -- > *James Paul Chibole* > -In the name of God- > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/CA%2B9bh8dc1ph6UC49pqjVeVfg-DEo%2BE2vFxu0NCXoB9AonsWXtQ%40mail.gmail.com > <https://groups.google.com/d/msgid/sqlalchemy/CA%2B9bh8dc1ph6UC49pqjVeVfg-DEo%2BE2vFxu0NCXoB9AonsWXtQ%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/CAJiyv_1ceFjyYTq7exyj2djJjfR%3DUUK7GqX4GXZ5p%2BF2E34PpQ%40mail.gmail.com.