Reputation: 8809
Due to some bad design decisions (not made by myself) I am left with a slight conundrum.
I had some code from a previous database query which was..
{{ order.pa_tranlength * order.pa_width * order.pa_costprice }}
This worked fine, however the database owner has now upgraded the database structure, and the last value order.pa_costprice is now a float, with the other two variables a decimal. Is there a way I can convert order.pa_costprice to a decimal on the fly mid calculation.
I have tried something like..
{{ order.pa_tranlength * order.pa_width * "%d" order.pa_costprice}}
Which obviously won't work as its a string value (I did test in my haste, but to no avail), but you can get the idea of what I am trying to do.
This loop is so large and comes directly from a database query, so I am trying to find a solution that means I don't have to modify any code before the query results hit the template.
Is there a workable solution from inside Jinja2 for this?
Upvotes: 2
Views: 1545
Reputation: 5682
You wouldn't be modifying the code of the query, but if you can make a code change, you could make a template filter:
@app.template_filter('to_decimal')
def to_decimal_filter(f):
return decimal.Decimal(f)
So:
{{ order.pa_tranlength * order.pa_width * order.pa_costprice }}
becomes:
{{ order.pa_tranlength * order.pa_width * order.pa_costprice|to_decimal }}
Upvotes: 2