Skip to Content
Menu
This question has been flagged
2 Replies
3995 Views

Hi all !

I have 2 class : dannh.sach.hd,  and danh.sach.huy.hd    . I want when I add : ma_hd into : danh.sach.huy.hd then  record  which has field : ma_hd same ma_hd in danh.sach.huy.hd ,in danh.sach.hd is deleted . I used method unlink.

my code py

class danh_sach_huy_hd(osv.Model):
    _name = 'danh.sach.huy.hd'
    
    _columns = {
                'stt':fields.char('STT',size = 5),
                'ma_hd':fields.many2one('danh.sach.hd','Mã hợp đồng', required = True),
                'ma_nv':fields.related('ma_hd','ma_nv', type = 'char',size = 10,string = 'Mã NV'),
                'ten_nv':fields.related('ma_hd','ten_nv', type = 'char',size = 10,string = 'Nhân viên'),
                'ly_do_huy_hd': fields.char('Lý do hủy hợp đồng', size = 30, required = True),
                'ngay_huy':fields.date('Ngày hủy hợp đồng',required = True),
                }
    _defaults = {
                 'stt':lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'danh.sach.stt'), 
                   
                 }  

class danh_sach_hd(osv.Model):
    _name = 'danh.sach.hd'
    _rec_name = 'ma_hd'
    
    def unlink(self, uid, cr, ids, context=None):
        if context is None:
            context = {}
        
        ref = self.pool.get('danh.sach.huy.hd').browse(cr, uid, 'ma_hd', context = context).ma_hd.id
        for rec in self.browse(cr, uid, ids, context = context):
            if rec.ma_hd == ref.ma_hd :
                return super(danh_sach_hd, self).unlink(cr, uid, rec.ma_hd.id, context=context)
    
    _columns = {
                
              'stt':fields.char('STT',size = 10),
              'ma_hd':fields.char('Mã HĐ',size=20,required=True),
              'ngay_ky':fields.date('Ngày ký',required=True),
              'ma_nv':fields.char('Mã NV',size=30),
              'ten_nv':fields.char('Nhân viên',size = 50,required=True),
              'ten_bp':fields.many2one('bo.phan', 'Bộ phận', required=False),
              'ma_bp':fields.related('ten_bp','ma_bp', type = 'char',size = 10, string = 'Mã BP'),
              'ten_cd':fields.many2one('chuc.danh','Chức danh',required=True),

}

 

So I worte it but it did not run as I want.

Anybody can help me? 

Avatar
Discard
Best Answer

use ondelete optional parameter for fields definition.

More info: 
https://doc.odoo.com/6.1/developer/03_modules_2/
http://stackoverflow.com/questions/17037330/restrict-no-action-set-default-in-ondelete-optional-parameter-for-fields
https://www.odoo.com/forum/help-1/question/ondelete-optional-parameter-19283

Avatar
Discard
Best Answer

You may want to try this to unlink it 

self.pool.get('danh.sach.hd')..unlink(cr, uid, rec.ma_hd.id, context=context)

 

I had similar problem when I tried to call unlink from a model or record set. The problem is that in Odoo 8.0, the method call is modified with inocrrect parameters for unlink -- just my guess. 

 

Avatar
Discard
Related Posts Replies Views Activity
1
Jun 20
4901
1
Jan 20
3013
1
Oct 22
1896
4
Oct 20
72834
6
Dec 19
17928