从控制器Odoo 10插入模型

2024-09-30 10:35:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要在我的Odoo网站上创建一个表单,这样外部用户就可以注册从我的Odoo发送的时事通讯。到目前为止,我已经创建了一个网站模板和一个控制器。到目前为止,我已经成功地显示了表单,做了一个Post,并预加载了许多额外的字段来填充表单,但是当我点击submit时,数据没有保存到数据库中。我看过网站的招聘模式有表格供人们填写,但我不知道数据保存在哪里以及如何保存。在

我包括以下来自我自己控制器的代码供参考:

我的控制器:

@http.route('/contact/register', type='http', auth='public', website='true')
def register(self, redirect=None, **post):

    areas_interest = request.env['iica_contacts.area_interest'].sudo().search([])
    topics_interest = request.env['iica_contacts.topic_interest'].sudo().search([])
    products_interest = request.env['iica_contacts.products_interest'].sudo().search([])
    countries = request.env['res.country'].sudo().search([])

    if post:
        return request.redirect('/contact/register/success')

    values = {
        'areas_interest' : areas_interest,
        'topics_interest' : topics_interest,
        'products_interest' : products_interest,
        'countries' : countries,
    }

    return request.render("iica_contacts.register", values)

我的模板:

^{pr2}$

我会非常感谢你的帮助。在


Tags: envregister表单search网站requestsudo控制器
1条回答
网友
1楼 · 发布于 2024-09-30 10:35:01

在调查和尝试了很多东西之后,我尝试了以下方法:

def register(self, redirect=None, **post):

    areas_interest = request.env['iica_contacts.area_interest'].sudo().search([])
    topics_interest = request.env['iica_contacts.topic_interest'].sudo().search([])
    products_interest = request.env['iica_contacts.products_interest'].sudo().search([])
    countries = request.env['res.country'].sudo().search([])

    values = {
        'areas_interest' : areas_interest,
        'topics_interest' : topics_interest,
        'products_interest' : products_interest,
        'countries' : countries,
    }

    if post:
        error_message, valid = self._validate_form(post)
        if valid == 1:
            self._process_registration(post)
            return request.redirect('/contact/register/success')
        else:
            return request.redirect('/contact/register/error')
    else:
        contact = request.env['iica_contacts.contact']
        values.update({
            'contact' : contact,
        })

    return request.render("iica_contacts.register", values)

插入模型的函数:

^{pr2}$

相关问题 更多 >

    热门问题