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

I need to restrict adding or deleting attachments in Purchase order when Purchase order is in Done state.It will getting error when i  inherit the create and unlink method of ir.attachment model.

from odoo import models, fields, api, _
from odoo.exceptions import UserError


class IrAttachment(models.Model):
_inherit = 'ir.attachment'

@api.model
def create(self, vals):
if vals.get('res_model') == 'purchase.order':
po = self.env['purchase.order'].browse(vals.get('res_id'))
if po.state == 'done':
raise UserError('Can not add Attachments in Locked State')
else:
return super(IrAttachment, self).create(vals)

@api.multi
def unlink(self):
if self.res_model == 'purchase.order':
po = self.env['purchase.order'].browse(self.res_id)
if po.state == 'done':
raise UserError('Can not Delete Attachments in Locked State')
else:
return super(IrAttachment, self).unlink()




is there any altranative solution for this

Avatar
Opusti

I am also interested, any suggestion ?

Best Answer

Hii Sreejishnu,


Please find the below logic to restrict adding and deleting the attachments in the model Purchase Order.

Below logic is defined in the ‘purchase.order’ itself. And the Logic is implemented under Write function which works on both Adding or Deleting the Attachment data in the Locked PO record. 

Please find code in comment. 

Thanks & Regards,
Email:  odoo@aktivsoftware.com   

Skype: kalpeshmaheshwari

Avatar
Opusti

Please find code here :-

class PurchaseOrder(models.Model):
_inherit = 'purchase.order'

def write(self, values):
for rec in self:
if rec.state == 'done':
for val in values.keys():
field = self.env['ir.model.fields'].search([('name','=',val),('model_id.model','=','purchase.order')])
related_model = field.relation
if related_model == 'ir.attachment':
raise UserError('Can not Add or Delete Attachments in Locked State')

return super(PurchaseOrder, self).write(values)

Related Posts Odgovori Prikazi Aktivnost
0
dec. 18
3912
2
mar. 25
5832
4
dec. 22
4390
1
nov. 22
17671
2
apr. 22
2938