如何在odoo 12中以Web形式将多个记录存储到One2many字段中

时间:2019-11-01 10:56:46

标签: python odoo erp odoo-12

我有一个Web窗体,其中包含多个选择选项字段,该字段具有各种值,如何将它们存储到Web控制器中的one2many字段中?

这是我要实现的目标的屏幕截图

enter image description here

我希望能够将用户填写的那些记录存储到store_line字段中

class CreateRequest(models.Model):
    _name = 'store.request'
    _inherit = 'mail.thread'

    reference = fields.Char(default=lambda self: _('New'),)
    request_date = fields.Date()
    approve_date = fields.Date()
    refuse_request = fields.Char()
    amount_transfers = fields.Integer(compute='_compute_count_transfers')
    amount_rfq = fields.Integer(compute='_compute_count_rfq')
    transfered = fields.Boolean(default=False)
    rfq = fields.Boolean(default=False)
    section = fields.Many2one('hr.employee.category', domain=[('display_req',
                                                               '=', True)])
    # request_by = fields.Many2one('hr.employee', related='section.category_ids')
    request_by = fields.Many2one('hr.employee', domain=[('category_ids', '=', 'section')])
    store_line = fields.One2many('store.request.line', 'number',
                                 string='Store Request', copy=True,
                                 index=True)

这是我的网络控制器

@http.route('/page/store_submit_form', auth='user', website=True)
    def submit_form(self, **kw):
        logging.info(kw)

        # i = 0
        # for key in kw:
        #     i
        #     logging.info(key)
        #     logging.info(kw[key])

        section = kw['section']
        employee_id = kw['requested_by']
        date = kw['date']
        product_id = kw['product']
        logging.info(product_id)
        description = kw['desc']
        quantity = kw['quantity']
        uom_id = kw['uom']
        lines = [(0, 0, {
            'name': int(product_id),
            'description': description,
            'quantity': int(quantity),
            'unit_of_quantity': uom_id
        })]
        exp_sheet_model = http.request.env['store.request']
        date = datetime.strptime(date, '%Y-%m-%d %H:%M:%S').date()
        data = {
            'section': section,
            'request_by': employee_id,
            'request_date': date,
            'is_webform': True,
            'store_line': lines,
            'state': 'draft'
        }
        logging.info(data)
        exp_sheet_model.sudo().create(data)

我正在获得lines变量中的值

1 个答案:

答案 0 :(得分:0)

您可以在表单的可选输入字段中使用相同的名称,例如productdescriptionquantityuom。 在POST控制器中,您可以使用以下getlist方法获取所选值的列表。

products = request.httprequest.form.getlist('product')
descriptions = request.httprequest.form.getlist('description')
quantities = request.httprequest.form.getlist('quantity')
uoms = request.httprequest.form.getlist('uom')

使用Python内置的zip生成组合的元组并遍历它:

lines = [(0, 0, {
            'name': int(product),
            'description': description,
            'quantity': int(quantity),
            'unit_of_quantity': int(uom)
        }) for product, description, quantity, uom in zip(products, descriptions, quantities, uoms)]

有关更多信息,请访问wrekzeug official documentation