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

I'm trying to populate a one2many relationship in a Wizard, the wizard contains a reference one2many to product categories(a Model). The Scenario is when I open the Wizard and I hit on a button (fill_product_category) it fill the list of product category in the tree view.

Hitting the button will build a list of product category in Backend and then assign it to the Wizard

The result is that only the first element of product category list is rendered in the wizard, whereas the others are not rendered. My code shows two static product categories but only the first one is rendered.

I need to show all the list what I should do?

thank you

1- The Wizard includs one2many relationship

<pre>

  class wizard(models.TransientModel):

    _name = 'simulator.wizard'

    y_product_category_disc_ids = fields.One2many("category.model", "id",string="Product Category")

  class CategoryModel(models.Model):

       _name = "category.model"

        y_product = fields.Char(string="Product Cat",readonly=True)

</pre>

2- Snippet of the Wizard Data file

       <field name="y_product_category_disc_ids"> <tree string="Product Category Tree"> <field name="y_product"/> </tree>

       </field> <button name="fill_product_category" string="Fill product category" type="object" />

3- function that handle the button fill_product_category

@api.multi def fill_one2many(self):

    self.ensure_one()

    res_record1 = self.env["purchase.product.categorydisc"].\

create({"y_product": p1 })

res_record2 = self.env["purchase.product.categorydisc"].\

create({"y_product": p2 })

listProdIds.append(res_record1.id)

listProdIds.append(res_record2.id)

self.y_product_category_disc_ids = listProdIds

return {

'context': self.env.context,

'view_type': 'form',

'view_mode': 'form',

'res_model': 'simulator.wizard',

'res_id': self.id, 'view_id': False,

'type': 'ir.actions.act_window',

'target': 'new',

}


Avatar
Discard

p1 and p2 are variables or string values?

Author

p1 and p2 are String

Best Answer

You need to use a Many2many field because One2many fields need a Many2one relation field to the current model in the target model of the One2many. With Many2Many fields you remove that contraints  

Avatar
Discard
Author

yes it works with many2many relation and the tree view of the sub elements is rendered. note that I couldn't define the two way references of many2one and one2many because a Model (e.g. CategoryModel) cannot define a reference to a Tranient Model, but a transient model can do it. any way thank you @Axel

Please then upvote the answer to help others