从one2many字段中删除记录时出现问题

问题描述 投票:0回答:2

我有一个“选举”模型,与其他相关模型“成员”有 3 个不同的 one2many 字段。这些 One2Many 字段之间的区别在于每个字段都有不同的域。问题是,当我尝试使用选举表单(使用树列表)从 one2many 字段之一删除一名成员时,当我单击选举表单中的“保存”按钮时,明显删除的记录会再次显示。也许这与存在 2 个不同的 one2many 关系这一事实有关,并且当您从一个关系中删除一个元素(成员)时,其他 One2many 字段中的引用完好无损。我正在使用 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>
odoo odoo-17
2个回答
0
投票

在我看来,你的模型设计应该修改: 您宁愿创建 2 个模型,而不是为成员和观察者创建一个模型“

elections.member
”:

  • elections.voter
  • elections.observer

...但是如果你有充分的理由保留你的模型设计: 因为一个 One2many 字段总是涉及关联代码模型中的 1 个 Many2One 字段,所以除了创建 3 个 Many2Many 字段(可以在数据库中创建 3 个不同的关系表以独立存储 3 种类型的关系)之外,您别无选择:


class Election(models.Model):
    _name = "election"
 
    member_ids = fields.Many2many(
        comodel_name='elections.member',
        relation='election_mbrnotobs_rel',
        column1='el_id',
        column2='elmbr_id',
        string='Members', domain=[('is_observer', '=', False)])
    
    observer_ids = fields.Many2many(
        comodel_name='elections.member',
        relation='election_mbrobs_rel',
        column1='el_id',
        column2='elmbr_id',
        string='Observers', domain=[('is_observer', '=', True), ('is_candidate', '=', False)])
    
    all_member_ids = fields.Many2many(
        comodel_name='elections.member',
        relation='election_mbrall_rel',
        column1='el_id',
        column2='elmbr_id',
        string='All members (including observers and candidates)')

0
投票

该问题是由于对所有三个 One2many 字段使用相同的 Many2one 字段 (election_id) 引起的。当您从 One2many 字段中删除一条记录时,Odoo 并不会真正从数据库中删除该记录 - 它只会更改引用字段 (election_id)。由于所有三个 One2many 字段均按域过滤,但共享相同的election_id 字段,因此该记录仍然存在,并在保存表单时重新出现。

class ElectionMember(models.Model):
    _name = 'elections.member'
    _description = 'Election Member'

    election_id_member = fields.Many2one('elections.election', string='Election (Members)')
    election_id_observer = fields.Many2one('elections.election', string='Election (Observers)')
    election_id_all = fields.Many2one('elections.election', string='Election (All Members)')
    # Other fields...


class Election(models.Model):
    _name = 'elections.election'
    _description = 'Election'

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

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

    all_member_ids = fields.One2many(
        'elections.member', 'election_id_all', string='All Members (including Observers and Candidates)'
    )
© www.soinside.com 2019 - 2024. All rights reserved.