0

我创建了一个模块来简单地为客户添加新的电话号码,并自动(ONCHANGE 功能)从另一个模型中识别电话号码的电话中心,并通过从数据库中搜索输入正确的电话中心名称。

所以这是我的 .PY 代码:

        # -*- coding: utf-8 -*-

    from openerp import models, fields, api, http


    class Contacts(models.Model):
        _name = 'isp.contacts'

        name = fields.Many2one('res.partner', string="Owner")
        telnumber = fields.Char(string="Tel Number", onchange='onchange_telnumber')
        telcenter = fields.Char(string="Tel Center", required=True)

        @api.onchange('telnumber')
        def onchange_telnumber(self):
            if self.telnumber:
                for record in self:
                    record.telcenter = (self.env.cr.execute(
                        'SELECT name FROM isp_precode WHERE name LIKE %record%'))


    class Telcenter(models.Model):
        _name = 'isp.telcenter'

        name = fields.Char('Center Name', required=True)
        ostan = fields.Char('Provience', required=True)
        city = fields.Char('City', required=True)
        manager = fields.Char('Manager', required=True)
        tel1 = fields.Char('Tel Number 1', required=True)
        tel2 = fields.Char('Tel Number 2', required=True)
        mdf_manager = fields.Char('MDF Manager', required=True)
        mdf_tel = fields.Char('MDF Tel', required=True)
        net_manager = fields.Char('Net Manager', required=True)
        net_tel = fields.Char('NET TEL NUMBER', required=True)


    class Precode(models.Model):
        _name = 'isp.precode'

        name = fields.Char()
        telcenter = fields.Many2one('isp.telcenter',
                                    ondelete='cascade', string="Tel Center", required=True)


    class Ports(models.Model):
        _name = 'isp.ports'

        name = fields.Char(required=True)
        port = fields.Char(required=True)
        floor = fields.Char(required=True)
        raw = fields.Char(required=True)
        dslam = fields.Char(required=True)
        slot = fields.Char(required=True)
        dslamport = fields.Char(required=True)
        dslamip = fields.Char(required=True)
        mothercompany = fields.Many2one('product.category',
                                        ondelete='cascade', string="Mother Company", required=True)
        telcentername = fields.Many2one('isp.telcenter',
                                        ondelete='cascade', string="Tel Center", required=True)

这是我的 XML 视图:

    And my XML view is :


        <?xml version="1.0" encoding="UTF-8"?>
    <openerp>
        <data>

            <record model="ir.actions.act_window" id="contacts_list_action">
                <field name="name">Service Contacts</field>
                <field name="res_model">isp.contacts</field>
                <field name="view_type">form</field>
                <field name="view_mode">tree,form</field>
                <field name="help" type="html">
                    <p class="oe_view_nocontent_create">Create the Service Contacts</p>
                </field>
            </record>


            <menuitem id="main_service_menu" name="Services Phone"/>

            <menuitem id="service_menu" name="Service Contacts"
                      parent="main_service_menu"/>

            <menuitem id="contacts_menu" name="Contacts" parent="service_menu"
                      action="contacts_list_action"/>



    <record model="ir.actions.act_window" id="telcenter_list_action">
                <field name="name">Tel Center</field>
                <field name="res_model">isp.telcenter</field>
                <field name="view_type">form</field>
                <field name="view_mode">tree,form</field>
                <field name="help" type="html">
                    <p class="oe_view_nocontent_create">Create the Tel Center</p>
                </field>
            </record>


            <menuitem id="telcenter_menu" name="Tel Center" parent="service_menu"
                      action="telcenter_list_action"/>


                    <record model="ir.actions.act_window" id="precode_list_action">
                <field name="name">Pre Code</field>
                <field name="res_model">isp.precode</field>
                <field name="view_type">form</field>
                <field name="view_mode">tree,form</field>
                <field name="help" type="html">
                    <p class="oe_view_nocontent_create">Create the Pre Code</p>
                </field>
            </record>


            <menuitem id="precode_menu" name="Pre Code" parent="service_menu"
                      action="precode_list_action"/>

                    <record model="ir.actions.act_window" id="ports_list_action">
                <field name="name">Ports</field>
                <field name="res_model">isp.ports</field>
                <field name="view_type">form</field>
                <field name="view_mode">tree,form</field>
                <field name="help" type="html">
                    <p class="oe_view_nocontent_create">Create the Ports</p>
                </field>
            </record>


            <menuitem id="ports_menu" name="PORTS" parent="service_menu"
                      action="ports_list_action"/>

        </data>
    </openerp>

4

1 回答 1

2

首先,进入 select %record% 被视为文字,而它应该被参数化。

其次,根本不要将游标用于原始查询,而是使用 ORM:

    @api.onchange('telnumber')
    def onchange_telnumber(self):
        self.telcenter = self.env['isp.telcenter'].search([('name','like',self.telnumber)]).name
于 2016-02-29T15:54:20.433 回答