Skip to Content
Menu
You need to be registered to interact with the community.
This question has been flagged
1 Odgovori
518 Prikazi

I have an 'election' model with 3 different one2many fields to other relatet model 'member'. The difference between those One2Many fields is that  each of them has a different domain. The problem is that, when I tried to delete one member from one of the one2many fields using the election form (using the tree list) the aparently deleted record shows again when I click the 'save' button from the election form. Perhaps it has something to do with the fact that there are 2 different one2many relations and when you delete an element  (member) from one, The reference is intact in the other One2many fields. I am working with Odoo 17

member_ids = fields.One2many('elections.member', 'election_id', string='Members',                                 domain=[('is_observer','=', False )])        

observer_ids = fields.One2many('elections.member', 'election_id', string='Observers',                              domain=[('is_observer','=', True ), ('is_candidate','=', False )]) 

all_member_ids = fields.One2many('elections.member', 'election_id', string='All members (including observers and candidates)')
 <notebook>
            <page name="members" string="Members">
                <field name="member_ids" readonly='state != "draft"'  domain="[('is_observer','=', False ), ('is_candidate','=', False )]" context="{'default_is_observer': 0, 'default_is_candidate': 0, 'from_o2m': 1}">
                  <tree>
                    <field name="name" />
                    <field name="specialty_certificate" />
                    <field name="membership_status" />
                    <field name="is_candidate" />
                    <button name="action_send_email_with_qr_code" type="object" string="Email with QR"
                            invisible="membership_status!='college'"
                            icon="fa-envelope"/>
                    <button name="viixoo_cmim_elections.small_qr_report" type="action" string="Print QR"
                            invisible="membership_status != 'college'"
                            icon="fa-print"/>
                  </tree>
                </field>
            </page>
            <page name="observers" string="Observers">
                <field name="observer_ids" readonly='state != "draft"'  string="Observer" domain="[('is_observer','=', True ), ('is_candidate','=', False )]" context="{'default_is_observer': 1, 'default_is_candidate': 0, 'from_o2m': 1}">
                <tree>
                    <field name="name" />
                    <field name="specialty_certificate" />
                    <field name="membership_status" />
                    <field name="is_president" />
                    <button name="action_send_email_with_qr_code" type="object" string="Email with QR"
                            invisible="membership_status!='college'"
                            icon="fa-envelope"/>
                    <button name="viixoo_cmim_elections.small_qr_report" type="action" string="Print QR"
                            invisible="membership_status!='college'"
                            icon="fa-print"/>
                  </tree>
                </field>
            </page>
</notebook>
Avatar
Opusti

could you clarify your action of "trying to delete" one member from the field...
Do you mean "DELETE MEMBER" or do you mean "UNLINK MEMBER FROM FIELD"?

Best Answer

Hi, you can try this code below to address your issue. Add separate relation field in member model and change field type of all_member_ids from one2many to computed many2many.

// Election model
member_ids = fields.One2many('elections.member', 'election_id', string='Members', domain=[('is_observer','=', False )])       
observer_ids = fields.One2many('elections.member', 'election_observer_id', string='Observers', domain=[('is_observer','=', True ), ('is_candidate','=', False )])
all_member_ids = fields.Many2many('elections.member', 'election_member_rel', store=True, string="All members (including observers and candidates)", compute='_compute_members')

@api.depends('member_ids', 'observer_ids')
def _compute_members(self):
    for r in self:
        member_ids = []
        for line in r.member_ids:
            member_ids.append(line.id)
        for ln in r.observer_ids:
            member_ids.append(ln.id)
        r.all_member_ids = member_ids


// Member model (elections.member)

election_id = fields.Many2one('elections.model', string='Election Member')
election_observer_id = fields.Many2one('elections.model', string='Election Observer')
Avatar
Opusti