2
votes

Thank you in advance.

What I have:

  • A module with a list of values with their descriptions.
  • A module with a list of features with their descriptions.

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:

  • Make a temporal module in which I have two many2one variables, one for each field (one for the feature and another-one for the value)

  • Create a many2many field in my class module and display it.

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")
2
If I understand well, the issue is that when you click on "add an item", you get the window shown in your second link, and you want to create relationships while staying on the form view ?Unatiel
Hello. It actually takes me to an empty table (which is sync2ba.etim.relationships module's view I guess) and there I need to clic and add a new relationship between feature and value). Stackoverflow didn't allow me to add 3 images haha. Anyway, I need to get rid of that second view. In my main Classes view I want to have a table with a row for the feature and another-one for the value and when I clic add new item I should be able to select those items and a pop up.Trody
Yes your second (in your post) picture indeed looks like the default form for sync2ba.etim.relationships. The strings don't match with the strings defined in the model, so they're probably overriden in your view. I'm a bit confused when you way you want to get rid of the the second view, but in your last sentence you say you "should be able to select those items and a pop up". Is the popup to add a new item in the table intended or not ?Unatiel
I have the first image, then I have imgur.com/a/KqqDJ and then I have the second image. I want to get rid of imgur.com/a/KqqDJ.Trody
Okay, so we're all talking about the same thing. I forgot about this intermediate popup.Unatiel

2 Answers

4
votes

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

0
votes

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.