【问题标题】:Pass list records to many2many field in odoo将列表记录传递给odoo中的many2many字段
【发布时间】:2021-10-08 08:35:12
【问题描述】:

我创建了一个wiz来获取“category_select”字段中的产品,请查看以下代码:

class CategorySelect(models.TransientModel):
    _name = 'product.group'
    category_select = fields.Many2many('product.category', string='Category')

    def start_search_product(self):
        wiz_form = self.env.ref('product_filter.get_products_form_all', False)
        for rec in self.category_select:
            product_res = self.env['product.product'].search([('categ_id', '=', rec.ids)])
        print('^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^', product_res)
        return {
            'type': 'ir.actions.act_window',
            'name': "Product Filtered",
            'res_model': 'product.selection',
            'target': 'new',
            'view_id': wiz_form.id,
            'view_mode': 'form',
            }
        }

我在这里所做的是我创建了一个返回向导的方法,但我没有得到一种方法,我应该如何将“product_res”传递给另一个称为返回菜单的向导中的 many2many 字段。

“任何帮助将不胜感激”

【问题讨论】:

    标签: python odoo odoo-14


    【解决方案1】:

    您可以在context 上设置默认 fields 设置您的值。

    ctx = self._context.copy()
    ctx.update({'default_m2m_fields': [(6, 0, IDS)]}) # on new wizard the m2m fields set with default on context
    

    【讨论】:

    • 好的,兄弟,我明白了……但我还有一个问题……如果我在 menuitem 中有相同的向导,在 sale_form 中有一个按钮,我想从所选产品中获取该产品的记录如果单击按钮,则类别到销售订单行,如果有人单击 menuitem,它将转到其他部分并返回返回部分的视图...该怎么做兄弟
    • 而且我已经更改了代码,我通过在搜索方法中提供 .ids 从搜索中获取所有产品
    • @SidharthPanda 最好您可以将其视为新问题,并以最佳方式(代码/图像)与代码分享您的尝试。对于未来有人可以得到这个答案是最好的问题。所以请创建一个我很乐意回答的新问题。
    【解决方案2】:

    您可以使用上下文将 product_res 传递给另一个向导

    1/ 更新您的 start_search_product 以传递 product_res id:

    def start_search_product(self):
        product_res_ids = [p.id for p in product_res]
        context = {'product_ids': product_res_ids}
        return {
            'type': 'ir.actions.act_window',
            'name': "Product Filtered",
            'res_model': 'product.selection',
            'target': 'new',
            'view_id': wiz_form.id,
            'view_mode': 'form',
            'context': context,
            }
        }
    

    2/ 在 ProductSelection 向导的 default_get 函数中获取产品 ID:

    product_ids = fields.Many2many('product.product', 'Products')
    
    @api.model
    def default_get(self, default_fields):
         res = super(ProductSelection, self).default_get(default_fields)
         product_ids = self._context.get('product_ids')
         res.update({'product_ids': [(6, 0, product_ids)]})
         return res
         
    

    【讨论】:

    • 谢谢兄弟...这节省了我很多时间
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-16
    • 1970-01-01
    • 2012-09-26
    • 2021-05-05
    • 1970-01-01
    相关资源
    最近更新 更多