How to filter records per month in Python / Flask

Asked

Viewed 138 times

0

Hey, guys. All right?

I started learning programming some time ago and took advantage of this quarantine to give a gas and manage to create my first system - a voucher/advance manager. It’s simple, the person requests a voucher with a certain value and then, on Dashboard, all the vouchers requested by that person and the amount of her salary updated appears.

It turns out now I’m trying to make an improvement that I don’t know where to start. I want the person to select a month of the year and all vouchers requested in that selected month appear.

I know it’s a simple question, but I thought about it anyway and I didn’t get an answer. If you can help me I really appreciate it!

For the record, I’m using python and flask with Blueprints and sqlalchemy.

With what I have now I can display all the vouchers requested by the user in this way in the view:

@core.route('/dashboard')
@login_required
def dashboard():
    vales = Withdrawals.query.filter_by(solicitante = current_user)
    total_vales = 0
    total_qt_vales = 0

    for vale in vales:
        total_vales = total_vales + vale.value
        total_qt_vales = total_qt_vales + 1

    salario_total = current_user.salary - total_vales




    return render_template('dashboard.html', vales=vales, salario_total=salario_total, total_qt_vales=total_qt_vales)


1 answer

0


So I wanted a little more information to try to help you, but from what you’ve provided I’m going to present you with an alternative:

Are the vouchers you mentioned saved in your ne database? If yes, then you have a class that represents this template and its columns of attributes, so you could add, for example:

class Vale(db.Model):
   #... colunas
   mes = db.Column(#Aqui você pode receber como uma data(db.DateTime) ou de outra foma)

Therefore, whenever the user enters with a month, you make a search in the database:

exemplo = Vale.query.filter_by(mes=mes).all()

If you only want the vouchers for a particular month from a specific user (logged-in user), then probably the user model should have a column with a list of vouchers, and these vouchers, as mentioned above, will have your month that was requested registered in your bank, so just access them and filter them by the user.

  • 1

    Thank you very much! I know you may have missed several details, but your answer gave me a great idea that I think will work.

  • No problem brother, anything only talk, if you can give an up on the answer too , hugs

  • Oops! I already gave up here but as my rep is low does not appear :/ kk

Browser other questions tagged

You are not signed in. Login or sign up in order to post.