Skip to Content
Menu
This question has been flagged
2 Replies
7635 Views

I'm doing some web programming to display the most sold 3 products and their value on a dashboard.  I called a python function that returns a  dictionary of calculated values to display, from the javascript file(using _rpc). The qweb is called from js file. Things seem to working but I cannot display the value in the key,value pair of the dictionary.

My python file
@api.model
def compute_dashboard(self):
        order_line = self.env['sale.order.line'].search([('state', '=', 'sale')])
        totals = {}
        for record in order_line:
            totals[record.product_id.name] = totals.get(record.product_id.name,0) + record.price_total
        return_val['totals'] = totals
        print(return_val)
        return return_val


And my qweb file

                <div class="col-sm-6 col-md-6">
                    <div class="col-sm-6 col-md-6 task_box" style="border-right:0">
                        <b>Top Sellers</b>
                        <t t-foreach="values.totals" t-as="pairs"> <!--displays product names, but not in desc order or limited by 3-->
                        <t t-esc="pairs"/><br/></t>
                    </div>
                    <div class="col-sm-6 col-md-6 task_box" style="border-left:0">
                        <b>Total</b>
<!--what to write here to display corresponding values-->>
                    </div>
                </div>

The console.log() displays correct output

totals: {product 1: 420, product 2: 1165.52, product 3: 1547, product 4: 2000, product 5: 3200}

So my question is what to write in qweb, so as the products and values get displayed in the descending order of their values, limited by just top 3 products.(I only want top 3 products to be displayed)

Should I use lists instead of dictionaries?

Thanks in advance.

Avatar
Discard
Best Answer

In Py

        order_line = self.env['sale.order.line'].search([('state', '=', 'sale')])
        totals = {}          
        for record in order_line:
            totals[record.product_id.name] = totals.get(record.product_id.name,0) + record.price_total
        sort_total = sorted(list(totals.items()), key=lambda r: r[1], reverse=True)
        return_val['totals'] = sort_total[0:3]


In Qweb

<div class="col-sm-6 col-md-6 task_box" style="border-right:0">
                        <b>Top Sellers</b><br/><br/>
                        <t t-foreach="values.totals" t-as="pairs">
                        <t t-esc="pairs[0]"/><br/></t>
                    </div>
                    <div class="col-sm-6 col-md-6 task_box" style="border-left:0">
                        <b>Total</b><br/><br/>
                        <t t-foreach="values.totals" t-as="pairs">
                        <t t-esc="pairs[1]"/><br/></t>
                    </div>


Avatar
Discard
Best Answer

use query or search_read() instead of search().

self.env['sale.order.line'].search_read([('state', '=', 'sale')], order="id desc", limit=3)

or

self._cr.execute("select field1, field2, ..., from sale_order_line where state = 'sale' order by id desc limit 3")



Avatar
Discard