在Odoo中更新表单中的字段是否会改变另一个字段的值?

时间:2016-03-12 12:23:28

标签: openerp

我在Odoo开发了一个位置模块,我可以在其中添加国家/地区&与该国有关的国家。然后我在另一个模块中调用这个模块我必须这样做: - 当选择一个国家时,它应该自动填充状态字段中属于该国家的州。如何做到这一点?我试过Onchange功能,但没有用?我会提供我的模型和查看下面的代码。

from openerp import models, fields, api

class peniel(models.Model):
    _name='peniel'

    name = fields.Char(string="Name")
    product = fields.Many2one('product.template', string='Product', required=True)
    employee = fields.Many2one('hr.employee', string='Employee', required=True)
    customer = fields.Many2one('res.partner', string='Customer', required=True)
    country = fields.Many2one('location', string='Country', required=True)
    state = fields.Many2one('state', string='State', required=True)
    date_d = fields.Date(string="Date Activity")
    comment = fields.Text(string="Comments")



   @api.onchange('country')
   def onchange_country(self):
    if self.country:
        self.state = self.country.state

查看文件将如下:

<?xml version="1.0" encoding="UTF-8"?>
<openerp>
    <data>
        <record id="action_peniel" model="ir.actions.act_window">
            <field name="name">Peniel </field>
            <field name="res_model">peniel</field>
            <field name="view_type">form</field>
            <field name="view_mode">tree,form</field>
            <field name="search_view_id" eval="False"/>
            <field name="context">{}</field>
            <field name="help">Create new Record</field>

        </record>

        <menuitem name="Peniel" id="peniel_roof" sequence="60"/>

        <menuitem id="menu_peniel_roof" name="Peniel" parent="peniel_roof" sequence="1"/>

        <menuitem action="action_peniel" id="menu_action_peniel" parent="menu_peniel_roof" sequence="20"/>

        <record id="view_location_tree" model="ir.ui.view">
            <field name="name">peniel.tree</field>
            <field name="model">peniel</field>
            <field name="arch" type="xml">
                <tree string="Peniel">
                    <field name="name"/>
                    <field name="product"/>
                    <field name="employee"/>
                    <field name="customer"/>
                    <field name="country"/>
                    <field name="state"/>
                    <field name="date_d"/>
                    <field name="comment"/>
                </tree> 
            </field>    

        </record>


        <record id="view_location_form" model="ir.ui.view">
            <field name="name">peniel.form</field>
            <field name="model">peniel</field>
            <field name="arch" type="xml">
                <form string="Peniel">
                    <group>
                       <field name="name"/>
                       <field name="product"/>
                       <field name="employee"/>
                       <field name="customer"/>
                       <field name="country"/>
                       <field name="state"/>
                       <field name="date_d"/>
                       <field name="comment"/>
                    </group>
                </form> 
            </field>    

        </record>       

    </data>
</openerp>  

1 个答案:

答案 0 :(得分:1)

他们不需要onchange功能。我们可以处理以下技巧。

替换字段定义

country = fields.Many2one('location', string='Country', required=True)
state = fields.Many2one('state', string='State', required=True)

country = fields.Many2one('res.country', string='Country', required=True)
state = fields.Many2one('res.country.state', string='State', required=True)

现在更改.xml方的字段:

替换字段

<field name="state"/>

<field name="state" domain="[('country_id', '=', country)]"/>