Selman
Selman

Reputation: 330

How can I use update method for each record with condidation?

for example I want to update the price of products with a price of zero dollars to 100.

for price_list_items in (self.env['product.pricelist.item'].search_read([], [])):

    if price_list_items.price == 0:
        price_list_items.price = 100

But It's not working.

AttributeError: 'dict' object has no attribute 'price'

How can I make such a update like this ?

Upvotes: 0

Views: 114

Answers (2)

Kenly
Kenly

Reputation: 26748

The search_read function returns a list of dicts, price_list_items is a dict of one record field values

Setting the price using the following syntax is available for recordsets:

price_list_recod.price = 100

Instead of fetching and reading all records, you can simply search for price list items with a price equal to 0.0 using the search method (which will return a recordset) and then update the price for all of them with one assignment

Example:

self.env['product.pricelist.item'].search([('price', '=', 0)]).price = 100

Upvotes: 1

Alexis Yovanofski
Alexis Yovanofski

Reputation: 54

you can do it this way :

list_of_pricelist = self.env['product.pricelist.item'].search([])

for pricelist in list_of_pricelist:
    if price_list_items.price == 0:
        price_list_items.price = 100

I think you better use search() in your case.

Difference between search() and search_read() :

https://www.odoo.com/fr_FR/forum/aide-1/difference-between-search-and-search-read-in-odoo-8-83076 Search_read() returns a list of dict.

If you really want to do your code this way, i think you can do it like this, but it's not usual

for price_list_items in (self.env['product.pricelist.item'].search_read([], [])):

    if price_list_items[<list_index:int>]['field_name'] == 0:
        price_list_items[<list_index:int>].update({'field_name': value})

Upvotes: 1

Related Questions