Skip to Content
Menu
This question has been flagged

In the base code of ./account_banking_payment_export/model/account_move_line.py

is a functional field delcared 


class account_move_line(orm.Model):
    _inherit = 'account.move.line'

    def amount_to_pay(self, cr, uid, ids, name, arg=None, context=None):
        """ Return the amount still to pay regarding all the payemnt orders
        (excepting cancelled orders)"""
        if not ids:
            return {}
        cr.execute("""SELECT ml.id,
                    CASE WHEN ml.amount_currency < 0
                        THEN - ml.amount_currency
                        ELSE ml.credit
                    END -
                    (SELECT coalesce(sum(amount_currency),0)
                        FROM payment_line pl
                            INNER JOIN payment_order po
                                ON (pl.order_id = po.id)
                        WHERE move_line_id = ml.id
                        AND po.state != 'cancel') AS amount
                    FROM account_move_line ml
                    WHERE id IN %s""", (tuple(ids),))
        r = dict(cr.fetchall())
        return r
        'amount_to_pay': fields.function(amount_to_pay,type='float', string='Amount to pay', fnct_search=_to_pay_search),

when i use this field in a loop...

for line in move_obj.browse(cr, uid, move_ids, context):
                if line.amount_to_pay > 0.00 

The moment it read the if line.amount_to_pay > 0.00 it generates a huge query on the postgres DB, i believe for all the account_move_line.

I was expecting just a query for one line, but this is not so...

Has anybody had the same experience with such an issue?

Avatar
Discard
Best Answer

Since I see your code, it is a bad prefetching to directly browse, loop through the browsed records and access their fields,  which kill the performance because ORM executes n number of queries to get this done ,  good prefetching is to browse all the records in a list first, then loop over the list, access the field and do the desired operation, which gives a good performance and avoid ORM to execute n number of queries instead use one query to get this done.

browsed_recs  = move_obj.browse(cr, uid, move_ids, context)

recs_list = [rec for rec in browse_recs]

for line in recs_list:

     if line.amount_to_pay > 0.00:

           #Do something


I hope It helps!

Avatar
Discard
Related Posts Replies Views Activity
4
Oct 16
29680
1
Sep 15
2667
1
Jun 15
111
0
Apr 15
81
2
Apr 15
270