如何在不执行超级写入的情况下覆盖写入方法? [英] How can override write method without executing the super write?

查看:89
本文介绍了如何在不执行超级写入的情况下覆盖写入方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在membership.py文件中,类account_invoice_line继承"account.invoice.line" 并重写write方法wich将在新发票行时创建新成员行 在现有发票中创建.对我来说,当创建新的发票行时,它是不正确的 在现有发票中,它必须与现有成员行相关,并且不能创建新成员行 所以我必须重写write methode,但是member.py文件中的write方法总是执行的问题 .有人可以告诉我如何重写write方法,而无需在Membership.py文件中传递write methode.这是文件Membership.py

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()

推荐答案

您可以如下所述绕过super方法的调用.

you can bypass calling of super method as like below.

您需要传递osv.osv类来调用直接基本写方法,而不是您的类名.

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)

它将绕过所有超级写入方法,并直接调用osv.osv类.

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

注意:您也可以调用其他任何类,而不是osv.osv.为此,您必须遵循以下步骤.

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

例如.

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

从此类系统中,将直接调用account_invoice类的写方法.

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

我希望它能对您有所帮助.

I hope it will help you.

这篇关于如何在不执行超级写入的情况下覆盖写入方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆