Odoo技术问询:在res.partner表单页签复用Scheduling树形视图
Hey there! Let's break down how to add that Scheduling tree view tab to your res.partner form step by step—I’ve tackled this exact scenario plenty of times, so here’s what you need to do:
1. First, set up the reverse One2many relation in res.partner
Since your custom Scheduling model has a Many2one field linking to res.partner, you need to add a corresponding reverse One2many field to the partner model. If you haven’t done this yet, create a model inheritance file (e.g., res_partner.py):
from odoo import models, fields class ResPartner(models.Model): _inherit = 'res.partner' scheduling_ids = fields.One2many( 'your.scheduling.model', # Replace with your actual Scheduling model technical name 'partner_id', # Replace with the field name in Scheduling that links to res.partner string='Schedulings' )
2. Modify the res.partner form view to add the notebook tab
You’ll need to inherit the existing res.partner form view and insert a new page into its notebook component. Here’s how the XML should look:
<odoo> <data> <!-- Inherit the base partner form view (adjust the inherit_id if you're using a custom one) --> <record model="ir.ui.view" id="partner_form_inherit_scheduling" inherit_id="base.view_partner_form"> <field name="arch" type="xml"> <!-- Insert the new page inside the notebook --> <xpath expr="//notebook" position="inside"> <page string="Schedulings"> <!-- Link to your One2many field, and specify your existing Scheduling tree view --> <field name="scheduling_ids" widget="one2many"> <!-- Point to your pre-existing Scheduling tree view's XML ID --> <tree view_id="your_module.scheduling_tree_view_id"/> <!-- Optional: Add a form view if you want to edit records directly from the tab --> <form view_id="your_module.scheduling_form_view_id"/> </field> </page> </xpath> </field> </record> </data> </odoo>
Key Details to Note:
- View ID: Replace
your_module.scheduling_tree_view_idwith the actual XML ID of your existing Scheduling tree view (e.g.,my_custom_module.scheduling_tree). - Automatic Domain Filtering: Since this is an
One2manyfield, Odoo will automatically apply a domain like[('partner_id', '=', active_id)]—so only records linked to the current partner will show up. If you need extra filtering (e.g., only confirmed schedulings), add adomainattribute to the<field>tag:<field name="scheduling_ids" widget="one2many" domain="[('state', '=', 'confirmed')]"> - Widget: The
widget="one2many"is optional (Odoo auto-detects it for One2many fields) but explicit is better for clarity.
3. Test the setup
Upgrade your module, open any res.partner form, and you’ll see the new "Schedulings" tab with your custom tree view, showing only records linked to that partner.
内容的提问来源于stack exchange,提问作者Marcio Cruz

