Re: [sqlalchemy] Re: Working with func in dates, No response, no error message
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 wrote: > Nancy Andeyo 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.
Re: [sqlalchemy] Working with func in dates, No response, no error message
Thank you Philip for your suggestion. On Thursday, March 30, 2023 at 9:38:08 PM UTC+3 Philip Semanchuk wrote: > > > > On Mar 30, 2023, at 2:32 PM, James Paul Chibole > wrote: > > > > Hi everyone, I am trying to retrieve deceased persons who died in the > current month but the output gives no result. Here is my code with query > done in Python Flask: > > from datetime import datetime from sqlalchemy import func > > @app.route('/user/') @login_required def user(username): > current_month = datetime.today().date().strftime("%B") > monthly_anniversaries = > current_user.followed_deaths().filter(Deceased.burial_cremation_dat e > f_death== current_month)).order_by(Deceased.timestamp.desc()) return > render_template("user.html", monthly_anniversaries =monthly_anniversaries) > > > Flask is an extra layer of complication here that’s getting in the way of > what you’re trying to debug. That’s not a knock on Flask (I use it too), > it’s just not germane to a SQLAlchemy problem. My suggestion is that you > try putting a breakpoint in your flask app so you can play with the query > inside the debugger. Ensure that current_month is what you think it is, > hardcode query params instead of passing variables to see if that changes > results, remove some of the filter clauses to see if the results change the > way you expect, etc. And of course having a look at the SQL that’s being > sent to the server will give you some clues too, although that can be hard > to get to depending on your environment. > > > Hope this helps > Philip -- 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/f6a17840-cdfd-4f26-bdc5-0a75af82fa91n%40googlegroups.com.
[sqlalchemy] Working with func in dates, No response, no error message
Hi everyone, I am trying to retrieve deceased persons who died in the current month but the output gives no result. Here is my code with query done in Python Flask: from datetime import datetime from sqlalchemy import func @app.route('/user/')@login_required def user(username): current_month = datetime.today().date().strftime("%B") monthly_anniversaries = current_user.followed_deaths().filter(Deceased.burial_cremation_dat e 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/3b972e00-e172-402a-983d-30ef7e22aef7n%40googlegroups.com.