1

I have a one2many widget that I need to have a condition when the user deletes any row.

Is there an on delete method that I can override? I have tried with unlink method but it doesn't get executed by the one2many widget. And the row are still being deleted.

<field context="{'medical_quotation_id': medical_quotation_id, 'outpatient_id': id}" name="medical_quotation_line_ids" widget="one2many_list">
    <tree string="Medical Quotation Lines" create="false">
        <field name="sequence" widget="handle" />
        <field name="medical_quotation_so_id" />
        <field name="dealer" />
        <field name="product_id" invisible="1"
            on_change="onchange_product_id(product_id, product_uom_qty, False, name)" />
        <field name="name" />
        <field name="lot_id" />
        <field name="remark" />
        <field name="product_uom_qty"
            on_change="onchange_product_id(product_id, product_uom_qty, False, name)" />
        <field name="product_uom" options="{'no_open': True}" />
        <field name="price_unit" />
        <!-- <field name="tax_id" widget="many2many_tags" domain="[('parent_id','=',False),('type_tax_use','&lt;&gt;','purchase')]" /> -->
        <field name="discount" />
        <field name="price_subtotal" />
    </tree>
</field>


@api.multi
def unlink(self):
    # import ipdb; ipdb.set_trace()
    for rec in self:
        if rec.medical_quotation_so_id:
            raise Warning(_('Cannot delete the line that has been invoiced!'))
        if rec.dealer == 'system':
            raise Warning(_('Cannot delete rows created by the system!'))
    return super(MedicalQuotationLine, self).unlink()    

Any ideas?

strike_noir
  • 4,080
  • 11
  • 57
  • 100
  • Ah, Odoo 8 i was wondering why it shouldn't work your way, but yes in that version it wasn't calling `unlink` on One2many field deletes. Try to look into the parent model's `write` method, at least there should be something like those black magic triple commands, which can be used. – CZoellner Mar 12 '20 at 15:11
  • Are you suggesting to compare previous `One2many` with current `One2many`? Will try that, thanks. – strike_noir Mar 12 '20 at 18:01
  • 1
    No the write values will have triple commands. Just look into the docstring of Model.write(). There are the magic numbers for deletion documented. – CZoellner Mar 12 '20 at 19:31
  • https://stackoverflow.com/questions/9377402/insert-into-many2many-odoo-former-openerp you can find the magic numbers in the first response – Mohammed Janati Idrissi Mar 13 '20 at 16:04

1 Answers1

0

Try to apply in this way,

def unlink(self, cr, uid, ids, context=None):
    for res in self.browse(cr, uid, ids, context=context):
        if res.medical_quotation_line_ids: 
            raise osv.except_osv(_('Error!'), _('Cannot delete the line that has been invoiced!'))
    return super(MedicalQuotationLine, self).unlink(cr, uid, ids, context=context)

Thanks

Dipen Shah
  • 2,396
  • 2
  • 9
  • 21