简体   繁体   中英

Open ERP- Functional field not getting called from code

I am trying to create a function field which will get the current membership type of a member and store it in a new field in res.partner. However this code is not getting called whenever I am creating or editing membership of a member. But if I remove the store attribute the functional field works just as expected. Note that I am reusing the membership module of openerp and using odoo8 now. I am attaching the code, please let me know where am I going wrong. I need this method to be called atleast when I am using the store attribute. Am I using the store attribute incorrectly:

from openerp.osv import osv, fields

class partner_member(osv.Model):
    '''Partner'''
    _inherit = 'res.partner'

    def _get_membership_type(self,cr,uid,ids,context=None):
        member_line_obj = self.pool.get('membership.membership_line')

        partner_list = []
        for line in member_line_obj.browse(cr, uid, ids, context=context):
            if line.state == 'paid':
                partner_list.append(line.partner.id)

        return partner_list

    def _current_membership(self, cr, uid, ids, field_name= None, arg=False, context=None):
        res_obj =  self.pool.get('res.partner')
        res_data_obj = res_obj.browse(cr, uid, ids, context=context)

        res=dict()

        for member in res_data_obj:
            if member.member_lines:
                for lines in member.member_lines:
                    if (lines.state == 'paid'):
                        res[member.id] = lines.membership_id.name_template
                        break
                    else:
                        res[member.id] = 'None'
            else:
                res[member.id] = 'None' 

        return res

    _columns = {
        'current_membership':
            fields.function(_current_membership,type='char',
                            string='Current Membership Type',
                            store = {
                                'membership.membership_line':
                                    (_get_membership_type, ['state'], 10)
                            },
                            help="Shows the current membership of a user"),
    }

You made a mistake on the _get_membership_type() method. Indeed, you return only the list of res.partner that are in a line with state == 'paid'.

I think you must return all the partner that are in lines no matter the state of the line.

def _get_membership_type(self,cr,uid,ids,context=None):
    member_line_obj = self.pool.get('membership.membership_line')

    partner_list = []
    for line in member_line_obj.browse(cr, uid, ids, context=context):
        partner_list.append(line.partner.id)

    return partner_list

If you want your function _current_membership to be a method of your class (as you did) you need to add the method=True parameter to your field definition:

_columns = {
    'current_membership':
        fields.function(_current_membership,type='char',
                        string='Current Membership Type',
                        store = {
                            'membership.membership_line':
                                (_get_membership_type, ['state'], 10)
                        },
                        help="Shows the current membership of a user",
                        method=True),
}

That should resolve your problem.

Certainly you can simply use store=True to have your field recalculated on every change in whatever field of your object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM