Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
211 Visualizzazioni

Hi,

I'm working on Odoo 18 and I’d like to filter two fields so that when I select one, I can see only the available services in the second field.

I believe I need to use a domain, but whether I try to edit it through XML or the domain editor, the changes I make only result in errors.

Using the domain editor, I’m able to create relationships to filter fields, but I can’t see the two specific fields—only the more “general” ones.

How can I view the correct fields or create this kind of relationship?

I'm using many2one fields.

Code: 

<data>

  <xpath expr="//field[@name='x_name']" position="attributes">

    <attribute name="force_save">1</attribute>

    <attribute name="placeholder"/>

    <attribute name="readonly">True</attribute>

    <attribute name="required">True</attribute>

    <attribute name="string">Nuovo Pasto</attribute>

  </xpath>

  <xpath expr="//group[@name='studio_group_ced1b4_left']" position="attributes">

    <attribute name="string">Scelta del centro cottura</attribute>

  </xpath>

  <xpath expr="//group[@name='studio_group_ced1b4_left']" position="inside">

    <field name="x_studio_sede" options="{&quot;create_name_field&quot;:&quot;x_name&quot;,&quot;no_create&quot;:true}" required="True"/>

  </xpath>

  <xpath expr="//group[@name='studio_group_ced1b4_right']" position="attributes">

    <attribute name="string">selezionare il servizio</attribute>

  </xpath>

  <xpath expr="//group[@name='studio_group_ced1b4_right']" position="inside">

    <field name="x_studio_servizio_associato_1" options="{&quot;create_name_field&quot;:&quot;x_name&quot;,&quot;no_create&quot;:true,&quot;no_open&quot;:true}" domain="[]"/>

    <field name="x_studio_one2many_field_83p_1ivnpm9br"/>

  </xpath>

  <xpath expr="//group[@name='studio_group_ced1b4']" position="after">

    <group name="studio_group_8oe_1ivnm11ti">

      <group name="studio_group_8oe_left" string="Numero pasti prodotti per servizio">

        <field name="x_studio_pasti_adulti"/>

        <field name="x_studio_pasti_bambini"/>

      </group>

      <group name="studio_group_8oe_right">

        <field name="x_studio_many2one_field_20q_1ivqabq8b"/>

      </group>

    </group>

    <group name="studio_group_2co_1ivnllih7" string="Info">

      <field name="create_date" force_save="1" readonly="True" widget="datetime" options="{&quot;show_seconds&quot;:false}" string="Creato da"/>

      <field name="write_date" force_save="1" readonly="True" string="Ultimo agg il"/>

      <field name="create_uid" force_save="1" readonly="True" string="Creato da"/>

      <field name="write_uid" string="Ultimo agg da"/>

    </group>

  </xpath>

</data>


Thank you :)

Avatar
Abbandona
Risposta migliore

Hii,

XML: Set the domain on x_studio_servizio_associato_1 field

In your XML view, you need to add the domain to the x_studio_servizio_associato_1 field to filter the available options based on the selected x_studio_sede field.

<field name="x_studio_sede" options="{'create_name_field': 'x_name', 'no_create': true}" required="True"/> <field name="x_studio_servizio_associato_1" options="{'create_name_field':

'x_name', 'no_create':

. Python: Use @api.onchange to dynamically update the domain

In the Python model file, you need to define a method using the @api.onchange decorator to update the domain of x_studio_servizio_associato_1 based on the selected x_studio_sede field.

Here’s the Python code to achieve this:

from odoo import models, fields, api class YourModelName(models.Model): _name = 'your.model' _description = 'Your Model Description' x_studio_sede = fields.Many2one('your.sede.model', string="Sede") x_studio_servizio_associato_1 = fields.Many2one('your.servizio.model', string="Servizio Associato") @api.onchange('x_studio_sede') def _onchange_x_studio_sede(self): """ This method will trigger when the x_studio_sede field changes. It will dynamically update the domain of the x_studio_servizio_associato_1 field. """ if self.x_studio_sede: return { 'domain': {'x_studio_servizio_associato_1': [('x_studio_sede', '=', self.x_studio_sede.id)]} } else: # Reset domain when x_studio_sede is empty or undefined return { 'domain': {'x_studio_servizio_associato_1': []} }

Restart your Odoo server to ensure the Python code is reloaded.

Update the module to apply changes to the XML view and Python code.

Test the form view:

  • Select a value for the x_studio_sede field.
  • The x_studio_servizio_associato_1 field should only display options related to the selected x_studio_sede.


i hope it is usefull

Avatar
Abbandona