Skip to Content
Menu
Dette spørgsmål er blevet anmeldt

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
Kassér

I am also interested, any suggestion ?

Bedste svar

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
Kassér

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 Besvarelser Visninger Aktivitet
0
dec. 18
4011
2
mar. 25
6101
4
dec. 22
4605
1
nov. 22
17979
2
apr. 22
3136