Creating Functional Fields using the new Odoo 8 APISubscription only
Odoo's powerful ORM allows you to create functional fields that are automatically re-calculated based on the conditions you specify.
Learn to use the power of functional fields in Odoo 8
Learn to use functional fields to provide a business solution
Learn the Odoo 8 API and Python datetime functions
Functional Fields in Odoo v8
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.