Search code examples
pythonxmlmany-to-manyodooodoo-12

Many2many field with repetition of the same record


I have a field in my custom model:

class custom_equipment_spec_soft(models.Model):
    _name = 'custom_maintenance.equipment.spec.soft'

    name = fields.Char(string='Name', required=True)

And I referenced it inside existing model like this:

soft_ids = fields.Many2many(comodel_name='custom_maintenance.equipment.spec.soft', relation='custom_maintenance_equipment_spec_soft_eq_rel', string='Software')

Then I added a view with a tree (table):

<group>
    <field name="soft_ids" widget="many2many">
        <tree>
            <field name="name"/>
        </tree>
    </field>
</group>

But now when I pick one record from the database inside the table I can not pick it for the second time. I would like to be able to pick the same record multiple times.

How do I do this? Is this the matter of changing the view widget or it has something to to do this a backend restriction?


Solution

  • What you can do is to create another model with Many2one relation to custom_maintenance.equipment.spec.soft model let's say spec.soft.line and add One2many relation to spec.soft.line from the existing model, in that case you can have same custom_maintenance.equipment.spec.soft record added multiple time.

    class SpecSoftLine(models.Model):
        _name = 'spec.soft.line'
    
        spec_soft_id = fields.Many2one('custom_maintenance.equipment.spec.soft')
        existing_model_id = fields.Many2one('existing.model.name')
    
    ##Existing model
    soft_ids = fields.One2many(comodel_name='spec.soft.line', inverse_name='existing_model_id')
    
    ##On existing model view add
    <group>
        <field name="soft_ids" >
            <tree editable="bottom">
                <field name="spec_soft_id"/>
            </tree>
        </field>
    </group>