This question has been flagged
6 Replies
14921 Views

Hii,

One2Many Custom Model

class PurchaseShipment(models.Model):
_name = "purchase.shipment"
    purchase_line_ids = fields.One2many('purchase.order.line', 'shipment_id', string='Shipment Lines')    ​

Many2one Purchase Order Line

class PurchaseOrderLine(models.Model):
_inherit = "purchase.order.line"
shipment_id = fields.Many2one('purchase.shipment', string='Shipment', copy=False, ondelete='set null')

I have a wizard that add purchase lines to purchase.shipment table. purchase.shipment view has a page and inside I show lines in a tree and after tree there is Add Lines button that calls wizard to add lines and it works ok.

class PurchaseShipmentLines(models.TransientModel):
_name = 'purchase.shipment.lines'
_description = 'Adds/Links purchase order lines to shipment for all selected lines'
def _default_shipment(self):
return self.env['purchase.shipment'].browse(self._context.get('active_id'))
shipment_id = fields.Many2one('purchase.shipment', string="Shipment", required=True, default=_default_shipment)
purchase_order_lines = fields.Many2many('purchase.order.line', string="Purchase Order Lines")
@api.multi
def add_to_shipment(self):
self.shipment_id.purchase_line_ids |= self.purchase_order_lines
return {}

Now I want to remove/unlink line(s) from purchase.shipment line tree. By default there is delete icon on line item tree but it try to delete from purchase.order.line model. How to unlink purchase line from purchase.shipment. Odoo v12.

Thanks,


Avatar
Discard
Best Answer

Hi,

Just change the paramater ondelete="set null" to ondelete="cascade" in your field declaration on purchase.order.line model.

CASCADE specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well.

shipment_id = fields.Many2one('purchase.shipment', string='Shipment', copy=False, ondelete='cascade')

Best regards

Avatar
Discard
Author

Hii Sylvain,

I changed ondelete='cascade' on purchase.order.line model as you said. What happened is when I deleted a line from purchase.shipment it deleted from purchase.order.line model as well. And that is not my requirement.

My requirement is to remove from purchase.shipment model only.

How to unlink or remove reference of One2many record from purchase.shipment.

Author

Here is Tree View

I want to unlink when deleting from this tree...

<field name="purchase_line_ids" nolabel="1">

<tree>

<field name="order_id" />

<field name="product_id" />

<field name="product_qty" />

<field name="qty_received" />

<field name="qty_invoiced" />

<field name="price_unit" />

</tree>

</field>

Best Answer

Hi,

The easiest way will be to create a new button on the tree view:

<button type="object" name="action_remove_shipment" icon="fa-chain-broken" string="Remove from Shipment"/>

Then in the purchase_order_line python file, add:

def action_remove_shipment(self):
    self.write({'shipment_id': False})   ​    ​   ​

Rather than deleting the purchase order line, it will just remove the link between the line and the shipment.

Avatar
Discard
Author

Thank you Jake Robinson. This is want I was looking for. Up Vote for you.

Author Best Answer

Override the child table unlink method worked for me....it will not delete the one2many record instead will remove from the one2many tree view. This method is executed on Save (edit the record will show trash/delete icon on tree, press it and delete).

@api.multi
def unlink(self): 
    self.write({'payslip_id': False})
    return True

Avatar
Discard