This question has been flagged

Hello,  I have a function field in a tree view that is supposed to display a calculated field, but for some reason the field is not being recalculated whenever I go to the tree view.  I know the function that calculates the field is not being called because I put a breakpoint in the beginning of the method, and it never stops on that breakpoint.  Does anyone know why a method for a functional field would not be called?  

Here is my field definition:

'days_remaining_func': fields.function(_get_days_remaining_func, method=True, type="integer", string="Days Remaining", store=True),


Here is my method:

def _get_days_remaining_func(self, cr, uid, ids, field_name, arg, context={}):

    # calculate the remaining days by subtracting the exp_date - todays date

    res = {}

    for rec in self.browse(cr, uid, ids):

        if not rec.support_exp_date:

            res.update({rec.id:{ field_name: None }})

            continue

        exp_date = datetime.strptime(rec.support_exp_date, "%Y-%m-%d")

        todays_date = datetime.now()

        days_remaining = exp_date - todays_date

        days_remaining = days_remaining.days

        res.update({rec.id:{ field_name: days_remaining }})

    return res

Avatar
Discard
Best Answer

The field does not recalculate because of it's defined with store=True

Avatar
Discard
Best Answer

HI Eric,

The issue suspected from your code is if your function method is call for only one field.

Than you can directly assigned return value to ID, you don't need to pass value with field. If your method is called for any new records means this is the fault.

Currently you did this.

 res.update({rec.id:{ field_name: None }}) 

Instead try this

res[rec.id] = your_val or None  

Or if your method is not called for already calculated record it means its store=true issue.

Here is the explanation of store=true..

When you define any functional field. than you will have

these possibilities :

when you set store=True it once the value of this will calculated will be stored in database and so ID of that record will not pass in list to recalculate if it is already calculated.

when  you haven't applied store=True than all the that ID will be pass to recalculate if any changes made on that record.

or

If you want to reconsecrate your information after storing in database than you can use store = {} (dictionary option)

Here in dictionary you pass the number of model and fields based on that your fields values is going to update and it will store as well.

The real example of store you will find in account module with account.invoice model. functional field with store= {}.

Any one who knows more about functional field than can improve my Answer.

Hope this will help.

Rgds,

Anil.








Avatar
Discard