Odoo 11 - 从其他模型中提取字段值

Odoo 11 - pull fields values from other models

我在 crm.lead 表单中添加了一个自定义字段 (many2one) 并为此添加了一个自定义字段 class

这是我的代码(我使用的是 odoo 11):

models.py

from odoo import models, fields, api

# Add fields to Leads
class Leads_events(models.Model):
    _inherit = 'crm.lead'

    venue = fields.Many2one('crm.lead.venue',String="Venue")
    venue_city = fields.Char(String="City")
    venue_country = fields.Many2one('res.country', string="Country")

# Create venue class
class Leads_venue(models.Model):
    _name = 'crm.lead.venue'

    name = fields.Char(string="Venue name")
    city = fields.Char(string="City")
    country = fields.Many2one('res.country', string="Country")
    address = fields.Text(string="Address")
    website = fields.Char(string="Website")
    phone = fields.Char(string="Phone")
    fax = fields.Char(string="Fax")
    notes = fields.Text(string="Notes")

views.xml

<record id="crm_lead_venue_form" model="ir.ui.view">
        <field name="name">crm.lead.venue.form</field>
        <field name="model">crm.lead.venue</field>
        <field name="arch" type="xml">
            <form string="Venues">
                <group>
                    <group>
                      <field name="name"/>
                      <field name="country"/>
                      <field name="city"/>
                      <field name="address"/>
                    </group>
                    <group>
                      <field name="phone"/>
                      <field name="fax"/>
                      <field name="website"/>
                    </group>
                    <group>
                      <field name="notes" />
                    </group>
                </group>
            </form>
        </field>
    </record>

<record id="crm.crm_case_form_view_oppor_events" model="ir.ui.view">
    <field name="name">crm_case_form_view_oppor_events</field>
    <field name="model">crm.lead</field>
    <field name="inherit_id" ref="crm.crm_case_form_view_oppor"/>
    <field name="arch" type="xml">    
        <xpath expr="//notebook" position="before">
            <group string="Venue">
                <field name="venue"/>
                <field name="event_city" string="City"/>
                <field name="event_country"/>
            </group>
        </xpath>
    </field>
</record>

这很好用。现在我要拉的是城市和国家从 crm.lead.venue selected 记录到 crm.lead 'venue_city' 和 'venue_country' 字段。

我查看了 onchange 函数,但找不到如何让它工作...

我在另一个 post 我读过之后试过这个,但它不起作用

@api.onchange('venue')
def onchange_venue(self):
    if self.venue:
        self.event_city = self.venue.city
        self.event_country = self.venue.country

有什么地方可以找到文档或更完整的帮助吗?官方文档对这个问题不是很准确

onchange 方法在用户更改视图中的字段时起作用,而我在您的视图中看不到该字段,因此永远不会调用该方法。您应该将 "venue" 字段添加到表单并再次测试。

编辑

你看到不是event_city而是venue_city了吗? onchange 应该在 leads_events 上。你测试过onchange被执行了没有值吗?或者它甚至不执行函数?

@api.onchange('venue')
def onchange_venue(self):
    if self.venue:
        self.venue_city = self.venue.city
        self.venue_country = self.venue.country