Se rendre au contenu
Menu
Cette question a été signalée

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
Ignorer

I am also interested, any suggestion ?

Meilleure réponse

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
Ignorer

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)

Publications associées Réponses Vues Activité
0
déc. 18
4027
2
mars 25
6114
4
déc. 22
4610
1
nov. 22
17991
2
avr. 22
3139