Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
3 Besvarelser
17260 Visninger

I have a one2many field in a model. This model form view shows the one2many field as a tree view , where there are inline fields. I want the one2many field to be a selection list like a many2one field. I want the user to select from a list instead of typing. How can I do this? 

Avatar
Kassér
Bedste svar

Hi,

If you want to select multiple records in a field, you can use the Many2many field in odoo.

Using the One2many field as a selection field is a false concept.


Thank you.


Avatar
Kassér
Bedste svar

Hi everyone,

If you need to enable multiple selection inside a One2many field, there is a module called "One2many Checkbox Widget" (for Odoo 17) available on the Odoo Apps store.

After installation, you can use it directly in your form view like this:

<field name="your_one2many_field" widget="one2many_checkbox"/>

This will add checkboxes for each line, allow selecting/deselecting all at once, and make the selected IDs accessible in the backend using:

self._context.get("selected_ids", [])

It’s a simple way to handle multi-selection in One2many fields.

Avatar
Kassér
Forfatter Bedste svar

Let me explain better. As you can see at the bottom of the image, the one2many field is shown as a tree view. However when user clicks on "Add item" a new row show up with text fields to add. But instead of text field I want it to be a list of all items from database so that user can only select from the list instead of a empty field. I tried adding widget="selection" but then I get an empty selection box.

<record id="iventiva_invoice_parameter_location_form" model="ir.ui.view">
<field name="model">account.invoice.parameter</field>
<field name="inherit_id" ref="iventiva_account_invoicing.account_invoice_parameter_form"/>
<field name="arch" type="xml">
<xpath expr="//sheet" position="inside">
<group string="Assigned Locationsssrrr">
<field name="stock_location_ids" nolabel="1">
<tree editable="bottom">
<field name="name" widget="selection"/>
</tree>
</field>
</group>
</xpath>
</field>
</record>


Avatar
Kassér

Then name field should be a Many2one field. not a char field.

Forfatter

Here is the basic class structure:

class A(models.Model):

_name = 'class.A'

associated_id = fields.Many2one('class.B')

name = fields.Char("Some name")

class B(models.Model)

_name = 'class.B'

a_ids = fields.One2many('class.A', 'associated_id')

in class B view, I have:

<field name="a_ids" nolabel="1">

<tree editable="bottom">

<field name="name"/>

</tree>

</field>

I want the name field to be a selection field containing all records of class A instead of a char field so that user does not have to type, just select from the list of A record names.

Related Posts Besvarelser Visninger Aktivitet
0
apr. 15
4158
1
mar. 15
9681
1
dec. 24
1776
0
jun. 19
3972
1
aug. 18
8288