Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Function field method is not called for tree view

By
Eric
on 8/26/15, 12:13 PM 1,065 views

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

1

Axel Mendoza

--Axel Mendoza--
10205
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 8/26/15, 9:08 PM

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

0

Anil R. Kesariya

--Anil R. Kesariya--
4321
| 6 5 8
Gandhinagar, India
--Anil R. Kesariya--
ERP Consultant

Key Skill
-
Technical & Functional Expert
Anil R. Kesariya
On 8/26/15, 11:11 PM

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.








Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 8/26/15, 12:13 PM
Seen: 1065 times
Last updated: 9/3/15, 8:48 PM