콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다

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

아바타
취소

I am also interested, any suggestion ?

베스트 답변

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

아바타
취소

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)

관련 게시물 답글 화면 활동
0
12월 18
3898
2
3월 25
5789
4
12월 22
4346
1
11월 22
17648
2
4월 22
2886