Odoo Help

0

For Loops inside return dictionaries

By
Gregor Heger
on 3/10/15, 5:44 AM 817 views

Hi,

I have searched through several sites, but I don't find how this "programming technique" is called.
To give an example: Inside the purchase module there is a function for counting all shipments for a purchase:

def _count_all(self, cr, uid, ids, field_name, arg, context=None):
        return {
            purchase.id: {
                'shipment_count': len(purchase.picking_ids),
                'invoice_count': len(purchase.invoice_ids),                
            }
            for purchase in self.browse(cr, uid, ids, context=context)
        }

I don't get why it is possible to put a for loop inside in a dictionary, and then, putting it even _AFTER_ the dictionary key purchase.id. Is there a name for this kind of programing technique so I can google it up? Or can anybode explain me, why it works to put it after purchase.id? Are dictionaries parsed by from the buttom to the top? Thanks for help!

0

Hi,

Your code is executed as like below notmal code.

res= {}

for purchase in self.browse(cr, uid, ids, context=context):

    res.update({purchase.id:{
                                             'shipment_count': len(purchase.picking_ids),
                                             'invoice_count': len(purchase.invoice_ids),  }

                                         })

return res

In your code all this things are written into {} (Dictionary) thats why looping is manage by {} (Dict) it self. We can also write loop inside of list objcet like res = [purchase.name for purchase in self.browse(cr, uid, ids, context=context) ]. So, here res is contain the list of name from all purchase order.

I hope it is useful to you.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

Register

Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)

Stats

Asked: 3/10/15, 5:44 AM
Seen: 817 times
Last updated: 3/16/15, 8:10 AM