Trody
Trody

Reputation: 423

How to make a Many2many field editable directly in a form view?

Thank you in advance.

What I have:

I want to make:

A class module in which the user can pair in a table one feature with a value.

The end goal would actually be to have in the same row a feature and all the values for that feature. I don't mind if I only get one value per row and need to enter the same feature multiples times for each value.

What I did:

What I get:

The form : Form view

When I click on "add an item" :

Item creation

What I know:

I think I should be able to easily make what I want but I tried to use inside my view and it doesn't work.

What I want to know:

How can I make a many2many field editable as if it was in a tree view ? That is to say, when I click on "add an item", an empty row is added, and then the user can fill the fields while staying on the form view.

If possible, how can I make it so I can set several values for the same feature ?

Code:

class Classes(models.Model):
    _name = 'sync2ba.etim.classes'
    name                    = fields.Char(string="Title", required=True)
    code                    = fields.Char(stCharring="Code")
    version                 = fields.Integer(string="Version")
    description             = fields.Char(string="Description")
    featurecode             = fields.Char(string="FeatureCode")
    status                  = fields.Char(string="Status")
    groupcode               = fields.Char(string="GroupCode")
    relationships           = fields.Many2many("sync2ba.etim.relationships", string="Features")

class Relationships(models.Model):
    _name = 'sync2ba.etim.relationships'
    name                    = fields.Char(string="Title", required=True)
    relationships_features  = fields.Many2one("sync2ba.etim.features", ondelete='set null', string="relationships_features", index=True)
    relationships_value     = fields.Many2one("sync2ba.etim.values", ondelete='set null', string="relationships_value", index=True)

class Features(models.Model):
    _name = 'sync2ba.etim.features'
    name                    = fields.Char(string="Title", required=True)
    code                    = fields.Char(string="Code")
    description             = fields.Char(string="Description")
    type                    = fields.Char(string="Type")

class Values(models.Model):
    _name = 'sync2ba.etim.values'
    name                    = fields.Char(string="Title", required=True)
    code                    = fields.Char(string="Code")
    description             = fields.Char(string="Description")

Upvotes: 2

Views: 5279

Answers (2)

Trody
Trody

Reputation: 423

Modifying the view file like this:

<label for="relationships"/>
<field name="relationships" widget="one2many">
    <tree editable="bottom">
        <field name="relationships_features"/>
        <field name="relationships_value"/>
    </tree>
</field>

Made the Classes view look like this:

enter image description here

Which was better than I expected. Check the comment to see the reasoning.

Upvotes: 2

Charif DZ
Charif DZ

Reputation: 14721

I don't know if it will work i needed the inverse of what you want for one2many field because one2many field when you click on add item an empty row is added.

In your view

           <field name="your_m2m_field" widget="one2many" />

For changing the behavior of o2m to m2m worked hope it wlrk for you too

EDIT :

Don't forget to make your tree view editable first it may work work without having to change the widget

        <field name="your_m2m_field" >
                <tree editable="bottom">
                        your list of fields here

I'm on my phone sorry for less code

Edits :

If you didn't find an easy way to do it what you have to do is that many2many field eventually it create a relation table between your two models just create that relation as a model and put two many2one field to your models an change many2many field to one2many to the new model and this way you did what many2many do. Hope you get the idea

Upvotes: 4

Related Questions