تخطي للذهاب إلى المحتوى
القائمة
لقد تم الإبلاغ عن هذا السؤال
6 الردود
17959 أدوات العرض

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,


الصورة الرمزية
إهمال
أفضل إجابة

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

الصورة الرمزية
إهمال
الكاتب

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.

الكاتب

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>

أفضل إجابة

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.

الصورة الرمزية
إهمال
الكاتب

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

الكاتب أفضل إجابة

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

الصورة الرمزية
إهمال
المنشورات ذات الصلة الردود أدوات العرض النشاط
1
يناير 25
1674
1
مايو 23
2665
2
سبتمبر 22
9417
2
أبريل 22
4443
0
يوليو 21
7136