Premium Video: Become an OdooClass.com subscriber to view the video below.

Creating Functional Fields using the new Odoo 8 API


Odoo's powerful ORM allows you to create functional fields that are automatically re-calculated based on the conditions you specify.

Video Length: 59 minutes Subscription only

Subscription Options

Learn to use the power of functional fields in Odoo 8

Odoo (formally OpenERP) has had functional fields available for several versions. In Odoo 8 however the functional fields API has had a complete overall that allows for more easily defining Odoo functional fields and making those functional fields more reliable. This video is just about an hour long and shows you how you can use functional fields to calculate the month to date sales order totals for your customers.

Learn to use functional fields to provide a business solution

Like many of our videos, this specific solution was out of a real world business request. The request is simple. When looking at a list of customer the client wanted to see a running total of month to date sales for each customer in the list. In this video we show how to satisfy this requirement using functional fields.

Learn the Odoo 8 API and Python datetime functions

If you are new to developing modules in Odoo 8 then you will want to look at some of the videos that are recommended at the bottom of this page. This is more of an intermediate Odoo video that goes into how to create functional fields and using python's datetime library to determine the start and end date of the current month. We learn a rather common method of determining the end date of a month given that different months have a different ending date depending on the number of days in that month.

What are functional fields?

Functional fields are fields that you add into your models in Odoo that are calculated using methods and functions you write in Python. For example, in this video we create a fucntional field that calculates month to date sales totals to display in your customer list. When the record is displayed, Odoo recognizes the field is functional and uses the python method to query the sales, total them and then update the field in the model. The beauty of this design is that the complexity of the calculation is encapsulated within the function and the view and model mechanisms within Odoo handle all the rest.

A real functional field example in Odoo

The example in this video and what we discuss below was from a real world requirement. Displaying month to date totals within the customer record. Often times in sales companies it is important to track month to date sales. This is where functional fields can make it easy to make that information available even though the stock Odoo applications don't have any way to provide it "out of the box".

How do I create a functional field?

In this video we use the Odoo 8 API to create functional fields. The syntax is actually pretty easy. Simply specify the function you need to call using the 'compute' parameter. In this case we have create a function called 'compute_mtd_sale'.

mtd_sales = fields.Float(compute='compute_mtd_sale', store=False,string='MTD Sales')

Creating the function and attention to perfromance in your functional fields

The code to create the function itself goes out to the sales module and grabs the required records. It is important to note that you only want to query the database for the records that you require. It is very bad form to grab all the sales order records and then use the Python code to filter out the records required for this month. Query only the desired records for best efficency. For functional fields this practice is more critical as performance must always be of great consideration. Always make sure you optimize fucntional field code as much as you can. In our example we have not completely optimized but we have taken care to only make sure we query the sales records we need for our calculations.

def compute_mtd_sale(self):
        cursor = self._cr
        user = self._uid
        
        today = datetime.datetime.today()
        start_date = str(today.replace(day=1).strftime('%m/%d/%Y'))
        next_month = today.replace(day=28) + datetime.timedelta(days=4)
        end_date = str(next_month - datetime.timedelta(days=next_month.day-1))
        print end_date 
        # Lookup the sales orders
        sale_obj = self.pool.get('sale.order')
        order_ids = sale_obj.search(cursor, user, [('date_order','>=',start_date),('date_order','<=',end_date),('partner_id','=',self.id),('state','!=','draft')])
        total = 0.0
        for sale in sale_obj.browse(cursor, user, order_ids):
            total += sale.amount_total
        self.mtd_sales = total

Putting it all together

If you are already familiar with Odoo development then this video will help you put Odoo functional fields to use. If you are brand new to Odoo / OpenERP development we recommend that you first take a look at our full Odoo Development Series... Mastering Odoo 9 Development.