Search code examples
pythonpython-3.xlistodooodoo-12

how to add values into odoo many2many field column depend on another column in the same field?


enter image description herei need to add a button to do a method that write a values into a many2many specific column

NOTE that values Equal another column value * self.standard_price in the product form

i tried to make list contains the values which i need to write it exactly but i can't to write those values into the specific column

class autopart(models.Model):
_inherit = 'product.template'

@api.one
@api.depends('car')
def test(self):
    test=[s.factor* self.standard_price for s in self.car]
    print(test)
    self.car.suggested_price = [(6,0,[test])]
    pass


car = fields.Many2many(comodel_name="cars", string="", )

XML

  <record id="product_auto" model="ir.ui.view">
      <field name="name">product.template.product.form</field>
      <field name="model">product.template</field>
      <field name="inherit_id" ref="product.product_template_only_form_view"/>
      <field name="arch" type="xml">
          <field name="qty_available" t-esc="'%.0f'%o.qty_available"/>
          <xpath expr="//page[@name='inventory']" position="after">
              <page name="pricing" string="Pricing" groups="master.auto_manger">
                  <group>

                      <field name="car">
                          <tree>
                              <field name="name"/>
                              <field name="model" widget="many2many_tags"/>
                              <field name="year"/>
                              <field name="factor"/>
                              <field name="suggested_price"/>
                          </tree>
                      </field>
                  </group>
                  <group>
                      <button name="test" class="oe_highlight" type="object" string="set values"/>
                  </group>

              </page>
          </xpath>
      </field>
  </record>

when i pree the button get this error " ValueError: Expected singleton: cars(1, 2) "


Solution

  • This error means that Car has more than one recordset.

    You will have to use for loop to update the value of suggested_price field. Please try the below code:

    @api.one
    @api.depends('car')
    def test(self):
        for c in self.car:
            c.suggested_price = c.factor* self.standard_price
    

    This will loop through each record and will update the suggested_price of each record in car model.

    Hope this help you.