Many2many 字段重复相同的记录

Many2many field with repetition of the same record

我的自定义模型中有一个字段:

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

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

我在现有模型中引用它是这样的:

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

然后我添加了一个带有树的视图(table):

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

但是现在当我从table里面的数据库中取出一条记录时,我不能第二次取出它。我希望能够多次选择同一条记录。

我该怎么做?这是更改视图小部件的问题还是后端限制?

你可以做的是创建另一个与 custom_maintenance.equipment.spec.soft 模型有 Many2one 关系的模型,假设 spec.soft.line 并添加 One2manyspec.soft.line 的关系现有模型,在这种情况下,您可以多次添加相同的 custom_maintenance.equipment.spec.soft 记录。

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>