maharshi maharshi - 3 months ago 10
Python Question

If loop continue is executing for loop again

if eqp_id:
for rule in items:
if rule.eqp_pricelist == True:
print "id 1"
print rule.id
continue
print "id 2 out"
print rule.id

#outputs:
#id1
#5
#id 2 out
#4


How is it possible that
rule.id = 5
comes before
rule.id = 4


This code is for product_pricelist for method :

def _price_rule_get_multi(self, cr, uid, pricelist, products_by_qty_by_partner, context=None):

Answer

As you noted in your comment, if items is acquired using: items = self.pool.get('product.pricelist.item').browse(cr, uid, item_ids, context=context) then items is a "recordset" of Odoo ORM, so you can use a sorted() function of Odoo ORM (see documentation):

sorted(key=None, reverse=False) Return the recordset self ordered by key.

Parameters:
key -- either a function of one argument that returns a comparison key for each record, or None, in which case records are ordered according the default model's order
reverse -- if True, return the result in reverse order

 

In order to apply this function to your code change it as follows:

if eqp_id:
    for rule in items.sorted(key=lambda r: r.id):  ## sort by id using sort()...
        if rule.eqp_pricelist == True:
            print "id 1"
            print rule.id
            continue
        print "id 2 out"
        print rule.id

 

EDIT:
I do not see clearly your goal, but check also filtered() if it can help you:

if eqp_id:
    for rule in items.sorted(key=lambda r: r.id).filtered(lambda r: r.eqp_pricelist == True):  ## sort by id using sort() AND filtered using eqp_pricelist == True...
        print "[True?] Rule with eqp_pricelist == %s" % rule.eqp_pricelist
        print rule.id

OR:

if eqp_id:
    for rule in items.sorted(key=lambda r: r.id).filtered(lambda r: r.eqp_pricelist == Talse):  ## sort by id using sort() AND filtered using eqp_pricelist == False...
        print "[False?] Rule with eqp_pricelist == %s" % rule.eqp_pricelist
        print rule.id

you can apply just filtered() without sorted() as well.

Check out this: for rule in items.filtered("eqp_pricelist"):