Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function that output Average Budget and Average Salary, the problem is when the computation executed for the average salary, also the average budget affected. I tried to use simple .copy()

routes.py:

@app.route("/user/<int:user_id>", methods=['GET', 'POST'])
@app.route("/<int:user_id>", methods=['GET', 'POST'])
@login_required
def user(user_id):
    budget_count = Budgets.query.filter_by(user_id=current_user.id).count()
    total_budget = current_user.current_budget
    added_budget = 0
    for budget in total_budget:
        added_budget = added_budget + budget.new_budget
    try:
        savings_budget = added_budget / budget_count
    except ZeroDivisionError:
        savings_budget = added_budget / 1

    average_budget = total_budget.copy()
    budget_sum = 0
    for average in average_budget:
        budget_sum = budget_sum + average.new_budget
    try:
        average_budget = budget_sum / budget_count
    except ZeroDivisionError:
        average_budget = budget_sum / 1
    
    user = User.query.get_or_404(user_id)
    return render_template("user.html", title=current_user.username, user=user, budget_count=budget_count, savings_budget=savings_budget, average_budget=average_budget)


@app.route("/expenses/<int:budget_id>", methods=['GET', 'POST'])
def expenses(budget_id):
    form = ExpenseForm()
    budget = Budgets.query.get(budget_id)
    if form.validate_on_submit():
        budget_expense = Expenses(categories=form.categories.data, category_cost=form.cost.data, budget_id=budget.id)
        db.session.add(budget_expense)
        db.session.commit()
        return redirect(url_for('expenses', budget_id=budget.id))
    posted_expenses = Expenses.query.filter_by(budget_id=budget.id)

    total = 0
    for post in posted_expenses:
        total = total + post.category_cost
    total_savings = budget.new_budget - total
    budget.new_budget = total_savings
    db.session.commit()
    return render_template("expenses.html", budget=budget, form=form, posted_expenses=posted_expenses, total_savings=total_savings)


What syntax or function to copy the data so that when the original data updated, the copy data not affected?

What I have tried:

I tried to append the data into a list then get the items in the list but it has the same output.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900