0

So I was wondering if there is a way to get a week or month period from DB. I have in my DB a value and a date and I want to get all items that their date are between for example 2020-03-01 and 2020-04-01.

I tried the following code but it gives a warning and doesn't return nothing:

The code:

users_count = StatsUsers.query.filter(and_(func.date(StatsUsers.created_date >= '2020-03-30'), func.date(StatsUsers.created_date <= '2020-04-30')))
        for x in users_count:
            print(x.value)
        return render_template('admin/index.html', users_count=0)

The warning that it gives:

Warning: (1292, "Incorrect datetime value: '1'")

My DB:

enter image description here

DeadSec
  • 808
  • 1
  • 12
  • 38

1 Answers1

0

I fixed my issue after reading this post: Flask-sqlalchemy query datetime intervals

What I did was basically change the filter(and_(func.date())) to only filter() and making sure the DB was in DATETIME.

The new code:

users_count = StatsUsers.query.filter(StatsUsers.created_date >= '2020-03-01 00:00:00').filter(StatsUsers.created_date <= '2020-04-30 00:00:00').all()
        for x in users_count:
            print(x.value)
DeadSec
  • 808
  • 1
  • 12
  • 38