从控制器插入模型 - Odoo 10

时间:2017-06-01 15:09:14

标签: python controller openerp odoo-10 odoo-website

我需要在我的Odoo网站上创建一个表单,以便外部用户可以注册从我的Odoo发送的新闻通讯。到目前为止,我已经创建了一个网站模板和一个控制器到目前为止,我已经设法显示表单并执行Post以及预加载我需要填写表单的一些额外的many2many字段,但是当我点击提交时数据不会保存到数据库中。我已经看到了website_portal和website_hr_recruitment模型,这些模型有人填写的表格,但我无法弄清楚数据保存在哪里以及如何完成。

我在自己的控制器中包含以下代码供参考:

我的控制器:

@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)

我的模板:

<template id="register">
<t t-call="website.layout">
  <t t-set="title">Register</t>
  <form action="/contact/register" method="post">
    <input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
    <div class="container">
        <div class="row">
          <div class="col-md-6 form-group">
            <label for="first_name">First name:</label>
            <input type="text" name="first_name" class="form-control" t-att-value="first_name" />
          </div>
          <div class="col-md-6 form-group">
            <label for="last_name">Last name:</label>
            <input type="text" name="last_name" class="form-control" t-att-value="last_name" />
          </div>
        </div>
        <div class="row">
          <div class="col-md-6">
            <div class="panel panel-default">
              <div class="panel-heading">Location information</div>
              <div class="panel-body">
                <div class="row">
                  <div class="col-md-12 form-group">
                    <label class="control-label" for="country_id">Country:</label>
                    <select name="country_id" class="form-control">
                      <option value=""> -- Select country -- </option>
                      <t t-foreach="countries" t-as="country">
                        <option t-att-value="country.id">
                          <t t-esc="country.name" />
                        </option>
                      </t>
                    </select>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-12 form-group">
                    <label for="company">Company:</label>
                    <input type="text" name="company" class="form-control" t-att-value="company" />
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-12 form-group">
                    <label class="control-label" for="sector">Sector:</label>
                    <select name="sector" class="form-control">
                      <option value=""> -- Select sector -- </option>
                      <option value="1">Public</option>
                      <option value="2">Private</option>
                      <option value="3">ONG</option>
                      <option value="4">International Organization</option>
                      <option value="5">Financial</option>
                      <option value="6">Other</option>
                    </select>
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-12 form-group">
                    <label class="control-label" for="language">Language:</label>
                    <select name="sector" class="form-control">
                      <option value=""> -- Select language -- </option>
                      <option value="1">Spanish</option>
                      <option value="2">English</option>
                    </select>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <div class="col-md-6">
            <div class="panel panel-default">
              <div class="panel-heading">Contact information</div>
              <div class="panel-body">
                <div class="row">
                  <div class="col-md-12 form-group">
                    <label class="control-label" for="email">Email:</label>
                    <input type="email" name="email" class="form-control" />
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-12 form-group">
                    <label class="control-label" for="phone">Phone number:</label>
                    <input type="tel" name="phone" class="form-control" />
                  </div>
                </div>
                <div class="row">
                  <div class="col-md-12 form-group">
                    <label class="control-label" for="contact_preference">Contact preference:</label>
                    <select name="contact_preference" class="form-control">
                      <option value=""> -- Select preference -- </option>
                      <option value="1">Telephone</option>
                      <option value="2">Email</option>
                      <option value="3">Text message (SMS)</option>
                      <option value="2">All of the above</option>
                      <option value="2">Dont contact me</option>
                    </select>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-md-12">
            <ul class="nav nav-tabs">
              <li class="active"><a href="#products-interest" data-toggle="tab">Products of interest</a></li>
              <li><a href="#areas-interest" data-toggle="tab">Areas of interest</a></li>
              <li><a href="#topics-interest" data-toggle="tab">Topics of interest</a></li>
            </ul>
            <div class="tab-content">
              <div id="products-interest" class="tab-pane fade in active">
                <h3>Products of interest</h3>
                <select name="products_interest" multiple="multiple" class="form-control">
                  <t t-foreach="products_interest" t-as="product">
                    <option t-att-value="product.id">
                      <t t-esc="product.name" />
                    </option>
                  </t>
                </select>
              </div>
              <div id="areas-interest" class="tab-pane fade">
                <h3>Areas of interest</h3>
                <select name="areas_interest" multiple="multiple" class="form-control">
                  <t t-foreach="areas_interest" t-as="area">
                    <option t-att-value="area.id">
                      <t t-esc="area.name" />
                    </option>
                  </t>
                </select>
              </div>
              <div id="topics-interest" class="tab-pane fade">
                <h3>Topics of interest</h3>
                <select name="topics_interest" multiple="multiple" class="form-control">
                  <t t-foreach="topics_interest" t-as="topic">
                    <option t-att-value="topic.id">
                      <t t-esc="topic.name" />
                    </option>
                  </t>
                </select>
              </div>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="col-md-12">
            <button type="submit" class="btn btn-default btn-primary">
              Register <span class="fa fa-long-arrow-right" />
            </button>
          </div>
        </div>
    </div>
  </form>
</t>

我真的很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

在调查并尝试了很多东西后,我设法通过以下方式实现:

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)

插入模型的功能:

def _process_registration(self, post):
    request.env['iica_contacts.contact'].create({
        'name' : post.get('name'),
        'company' : post.get('company'),
        'country_id': post.get('country_id'),
        'sector' : post.get('sector'),
        'language' : post.get('language'),
        'email' : post.get('email'),
        'phone' : post.get('phone'),
        'area_interest_ids' : self._process_many2may(request.httprequest.form.getlist('areas_interest')),
        'product_interest_ids' : self._process_many2may(request.httprequest.form.getlist('products_interest')),
        'topic_interest_ids' : self._process_many2may(request.httprequest.form.getlist('topics_interest')),
    })