从odoo网站点击按钮存储数据

时间:2015-08-17 08:30:40

标签: python database web openerp webpage

我想从odoo网页将数据存储到odoo数据库中。我已经从odoo网站构建器创建了一个新页面。它有一些输入字段和一个提交按钮。我想在点击提交按钮时将该字段的数据存储到数据库中的表中。 Odoo文档仅介绍如何从数据库读取数据到网页,而不是如何将数据从网页存储到数据库。有谁知道怎么做?

以下是我的代码:

控制器:

@http.route('/tasks/clocktime', type='http', auth='user', website=True)
    def clock_time(self, **post):
        task_pool = request.registry['project.task']
        task_pool.attendance_action_change()
        return

模板:

<form target="_self" action="/tasks/clocktime" method="post">
    <a class="btn btn-primary a-submit">Log In/Out</a>
</form>

1 个答案:

答案 0 :(得分:1)

单击“提交”按钮后,您必须路由到某个方法。在该方法中,您可以使用普通的odoo函数(如(create,write))将数据存储到数据库中。
你必须使用request.registry [&#39; model.name&#39;]。方法(......)
其中方法是根据要求创建/写入的 我正在粘贴来自website_sale模块的示例代码,该模块将数据写入sale_order模型

@http.route(['/shop/payment/transaction/<int:acquirer_id>'], type='json', auth="public", website=True)
    def payment_transaction(self, acquirer_id):
        cr, uid, context = request.cr, request.uid, request.context
        transaction_obj = request.registry.get('payment.transaction')
        order = request.website.sale_get_order(context=context)

        if not order or not order.order_line or acquirer_id is None:
            return request.redirect("/shop/checkout")

        assert order.partner_id.id != request.website.partner_id.id

        # find an already existing transaction
        tx = request.website.sale_get_transaction()
        if tx:
            if tx.state == 'draft':  # button cliked but no more info -> rewrite on tx or create a new one ?
                tx.write({
                    'acquirer_id': acquirer_id,
                    'amount': order.amount_total,
                })
            tx_id = tx.id
        else:
            tx_id = transaction_obj.create(cr, SUPERUSER_ID, {
                'acquirer_id': acquirer_id,
                'type': 'form',
                'amount': order.amount_total,
                'currency_id': order.pricelist_id.currency_id.id,
                'partner_id': order.partner_id.id,
                'partner_country_id': order.partner_id.country_id.id,
                'reference': order.name,
                'sale_order_id': order.id,
            }, context=context)
            request.session['sale_transaction_id'] = tx_id

        # update quotation
        request.registry['sale.order'].write(
            cr, SUPERUSER_ID, [order.id], {
                'payment_acquirer_id': acquirer_id,
                'payment_tx_id': request.session['sale_transaction_id']
            }, context=context)

        return tx_id