Ir al contenido
Menú
Se marcó esta pregunta
1 Responder
4237 Vistas

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
Descartar

I am also interested, any suggestion ?

Mejor respuesta

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
Descartar

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)

Publicaciones relacionadas Respuestas Vistas Actividad
0
dic 18
3784
2
mar 25
5643
4
dic 22
4199
1
nov 22
17485
2
abr 22
2714