Skip to Content
Menu
This question has been flagged
1 Reply
1048 Views

Hello, I was wondering if I have an object that I got trough a browse action like:

cats = self.env['product.category'].browse([1,2,3,4])

Now this object contains 4 category-lines.

Is it possible in the code to call like the category with id=4 like you can in a dictionary?

something like lines[4]?

Now I do it with a for loop, but this always takes 3 lines of code and looks sloppy.

for cat in cats:

    if cat.id=4:

        spec_cat = cat

or if I want the cats with the name='test',

test_cats = {}

for cat in cats:

    if cat.name ='test':

        test_cats[cat.id : cat]

I know you can do self.env['product.category'].search(['name', '=', 'test')]) or browse for the id search, but I got the feeling that this takes a new database action each time. In some for loops this would mean calling a database in a for loop. witch I think creates to much database actions.


Avatar
Discard
Best Answer

Hi

you can access recordset by index

eg: cats[3]

Hope this helps

regards

Avatar
Discard
Author

Thank you, this definitely helps. Now I always put them in a dict to get the same result. :-)

Is there also a way to be able to search inside a browse obj, on one of the fields?

Its better to go with the search function, if you don't know the correct id.