How can override write method without executing the super write?

谁说我不能喝 提交于 2019-12-11 09:08:35

问题


in membership.py file the class account_invoice_line wich inherits 'account.invoice.line' and override the write method wich will create a new member line when a new line of invoice is created in an existed invoice. For me its not correct when a a new line of invoice is created in an existing invoice it must be related to the existed member line and not create a new member line so i must override the write methode, but the problem that the write method in member.py file is always executed . So can someone tell me how to override the write method without passing by the write methode in membership.py file. This is the code in file membership.py

class account_invoice_line(osv.osv):
_inherit='account.invoice.line'

def write(self, cr, uid, ids, vals, context=None):
    """Overrides orm write method
    """
    member_line_obj = self.pool.get('membership.membership_line')
    res = super(account_invoice_line, self).write(cr, uid, ids, vals, context=context)
    for line in self.browse(cr, uid, ids, context=context):
        if line.invoice_id.type == 'out_invoice':
            ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', line.id)], context=context)
            if line.product_id and line.product_id.membership and not ml_ids:
                # Product line has changed to a membership product
                date_from = line.product_id.membership_date_from
                date_to = line.product_id.membership_date_to
                if line.invoice_id.date_invoice > date_from and line.invoice_id.date_invoice < date_to:
                    date_from = line.invoice_id.date_invoice
                member_line_obj.create(cr, uid, {
                                'partner': line.invoice_id.partner_id.id,
                                'membership_id': line.product_id.id,
                                'member_price': line.price_unit,
                                'date': time.strftime('%Y-%m-%d'),
                                'date_from': date_from,
                                'date_to': date_to,
                                'account_invoice_line': line.id,
                                }, context=context)
            if line.product_id and not line.product_id.membership and ml_ids:
                # Product line has changed to a non membership product
                member_line_obj.unlink(cr, uid, ml_ids, context=context)
    return res

def unlink(self, cr, uid, ids, context=None):
    """Remove Membership Line Record for Account Invoice Line
    """
    member_line_obj = self.pool.get('membership.membership_line')
    for id in ids:
        ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', id)], context=context)
        member_line_obj.unlink(cr, uid, ml_ids, context=context)
    return super(account_invoice_line, self).unlink(cr, uid, ids, context=context)

def create(self, cr, uid, vals, context=None):
    """Overrides orm create method
    """
    member_line_obj = self.pool.get('membership.membership_line')
    result = super(account_invoice_line, self).create(cr, uid, vals, context=context)
    line = self.browse(cr, uid, result, context=context)
    if line.invoice_id.type == 'out_invoice':
        ml_ids = member_line_obj.search(cr, uid, [('account_invoice_line', '=', line.id)], context=context)
        if line.product_id and line.product_id.membership and not ml_ids:
            # Product line is a membership product
            date_from = line.product_id.membership_date_from
            date_to = line.product_id.membership_date_to
            if line.invoice_id.date_invoice > date_from and line.invoice_id.date_invoice < date_to:
                date_from = line.invoice_id.date_invoice
            member_line_obj.create(cr, uid, {
                        'partner': line.invoice_id.partner_id and line.invoice_id.partner_id.id or False,
                        'membership_id': line.product_id.id,
                        'member_price': line.price_unit,
                        'date': time.strftime('%Y-%m-%d'),
                        'date_from': date_from,
                        'date_to': date_to,
                        'account_invoice_line': line.id,
                    }, context=context)
    return result

account_invoice_line()

回答1:


you can bypass calling of super method as like below.

Instead of your class name you need to pass osv.osv class to call direct base write method.

res = super(osv.osv, self).write(cr, uid, ids, vals, context=context)

It will bypass the all super write method and directly called of osv.osv class.

Note: You can also call any other class instead of osv.osv. For that you have to follow as like below.

for ex.

from openerp.addons.account.account_invoice import account_invoice


class account_invoice_inherit(osv.osv):
def write( . .. . )

    res = super(account_invoice,self).write(. .. .)

    . .. .

    return res

Here from this class system will called directly write method of account_invoice class.

I hope it will help you.



来源:https://stackoverflow.com/questions/30323752/how-can-override-write-method-without-executing-the-super-write

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!