OpenERP函數字段的應用
在ERP開發過程中經常會使用到某字段的值是由其他字段計算得來,並且有些還需要將計算的結果存入資料庫。
以上功能上OpenERP中是用field.function實現的
其中有種模式
a). 只計算,不存儲
這種方式比較簡單,只需要設定用來計算值的函數即可,函數分類method和function,method是指當前對象的方法,function是指一般的python函數,有特定簽名的函數
[python] view plaincopy
‘amount': fields.function(_amt,string='caption',method=True,type='float')
If method is true, the signature of the method must be:
def fnct(self, cr, uid, ids, field_name, field_value, arg, context):
otherwise (if it is a global function), it should be:
def fnct(cr, table, ids, field_name, field_value, arg, context):
b).計算,並且要將計算的結果存儲到資料庫,然後在相關的字段發生變更時進行重算
此模式需要指定store參數
store Parameter
It will calculate the field and store the result in the table. The field will be recalculated when certain fields are changed on other objects. It uses the following syntax:
store = {
'object_name': (
function_name,
['field_name1', 'field_name2'],
priority)
}
It will call function function_name when any changes are written to fields in the list ['field1','field2'] on object 'object_name'. The function should have the following signature:
def function_name(self, cr, uid, ids, context=None):
Where ids will be the ids of records in the other object's table that have changed values in the watched fields. The function should return a list of ids of records in its own table that should have the field recalculated. That list will be sent as a parameter for the main function of the field.
请注意,function_name必须要能返回主函数所需要的ids的列表,即主函数的对象ID列表,而不是object_name所指的对象id.
['field_name1', 'field_name2'],
执行时,当发生属性变更事件,会进行比对object_name以及['field_name1', 'field_name2'],是否match,当成功match,将执行function_name,并以当前发生变更的对象id为参数
Here's an example from the membership module:
'membership_state':
fields.function(
_membership_state,
method=True,
string='Current membership state',
type='selection',
selection=STATE,
store={
'account.invoice': (_get_invoice_partner, ['state'], 10),
'membership.membership_line': (_get_partner_id,['state'], 10),
'res.partner': (
lambda self, cr, uid, ids, c={}: ids,
['free_member'],
10)
}),
c).函數一次為多個計算字段提供值,設定multi='groupname'
multi is a group name. All fields with the same multi parameter will be calculated in a single function call.
函數的返回值格式需要調整為
[python] view plaincopy
{id:{'field1':value1},{'field2':value2},
id2:{'field1':value1},{'field2':value2}
}
這種模式比較不用擔心,如何去計算,何時去計算的問題,不過,如果計算值依賴於其他計算值時,就特別需要留意store裏面的priority參數
[python] view plaincopy
store = {
'object_name': (
function_name,
['field_name1', 'field_name2'],
priority)
}
为提高执行效率,减少代码执行次数,这是有效的办法
OpenERP函數字段的應用的更多相关文章
- Math.Round函數
Math.Round這個函數的解釋是將值按指定的小數位數舍入,但並不就是四捨五入.這種舍入有時稱為就近舍入或四舍六入五成雙 其實在 VB, VBScript, C#, J#, T-SQL 中 Roun ...
- Oracle排序函數Rank
出口給報關行出貨的時候,同一票shipment中合併多個invoice跟packing,轉出到廠商的報關系統時候,出口報關的序號會將invoice的序號做自動增加. 因為wafer會有出口給其他外包做 ...
- js正則函數 match、exec、test、search、replace、split 使用介紹集合
match 方法 使用正則表達式模式對字元串執行查找,並將包含查找的結果作為數組返回. stringObj.match(rgExp) 參數 stringObj 必選項.對其進行查找的 String 對 ...
- CopyFile函數詳解
CopyFile函數,文件拷贝函数.其基本結構如下: copyfile( lpcstr lpexistingfilename, // 源文件路径 lpcstr lpnewfilename, //新文件 ...
- Hive時間函數-年份相加減
Hive時間函數-年份相加減 目前為止搜了很多资料,都没有找到Hive关于时间 年份,月份的处理信息,所以就自己想办法截取啦 本来是用了概数,一年365天去取几年前的日期,后来测试的发现不够精准,然后 ...
- oracle decode(nvl(estimate_qty,0),0,1,estimate_qty) 函數
oracle decode(nvl(estimate_qty,0),0,1,estimate_qty) 函數
- js匿名函數
(function($){})(jquery) == (function($){})(jQuery) 实际上是匿名函数 用于存放开发插件的代码 作用(非常有用): 这种写法的最大好处是形成闭包.在(f ...
- C#判断奇偶数的函數
// 现代流行的"程序员" public static bool IsOdd(int n) { while (true) { switch (n) { : return true; ...
- PHP 計算字符串長度函數
PHP內置的字符串長度函數strlen無法正確處理中文字符串,它得到的只是字符串所占的字節數.對於GB2312的中文編碼,strlen得到的值是漢字個數的2倍,而對於UTF-8編碼的中文,就是3倍的差 ...
随机推荐
- 【洛谷】2990:[USACO10OPEN]牛跳房子Cow Hopscotch【单调队列优化DP】
P2990 [USACO10OPEN]牛跳房子Cow Hopscotch 题目描述 The cows have reverted to their childhood and are playing ...
- 内功心法 -- java.util.LinkedList<E> (6)
写在前面的话:读书破万卷,编码如有神--------------------------------------------------------------------下文主要对java.util ...
- 实用小工具 -- 国家地区IP段范围查询工具
如果想限制某个国家地区IP段访问,这几个查询工具就很有用了. 可以查询各个国家IP段范围,并且是持续更新的,使用方便. 当然,除此之外,你还可以通过APNIC.ARIN.RIPE这些官方IP分配机构查 ...
- PAT甲级1014. Waiting in Line
PAT甲级1014. Waiting in Line 题意: 假设银行有N个窗口可以开放服务.窗前有一条黄线,将等候区分为两部分.客户要排队的规则是: 每个窗口前面的黄线内的空间足以包含与M个客户的一 ...
- Xcode常用插件推荐
1.Xcode插件管理工具–Alcatraz的安装 Alcatraz是针对Xcode的一款插件管理器,通过Alcatraz可以非常方便的管理插件,包括安装.删除.升级等操作. 官方网站 安装方法一(推 ...
- Git配置非22端口,解决:ssh: connect to host xxx port 22: Connection timed out fatal: The remote end hung up unexpectedly
背景:私自搭建了Git服务器,而Git本身就是SSH进行连接的,而Git命令上默认只能通过22端口实现. 解决方法: 第一种: 在系统的用户目录下的文件夹:.ssh 如果该路径下没有config文件, ...
- HTML5项目笔记4:使用Audio API设计绚丽的HTML5音乐播放器
HTML5 有两个很炫的元素,就是Audio和 Video,可以用他们在页面上创建音频播放器和视频播放器,制作一些效果很不错的应用. 无论是视屏还是音频,都是一个容器文件,包含了一些音频轨道,视频轨道 ...
- Apple Developer申请成功
上周日白天,我去申请了Apple Developer.我先是在百度上浏览了一些经验教程,但是点进苹果开发者官网时却发现完全不是那么一回事.盖因它的页面经常在变,如同现在苹果在主推tvOS这个对中国用户 ...
- Go语言设计模式实践:迭代器(Iterator)
关于本系列 决定开个新坑. 这个系列首先是关于Go语言实践的.在项目中实际使用Go语言也有段时间了,一个体会就是不论是官方文档.图书还是网络资料,关于Go语言惯用法(idiom)的介绍都比较少,基本只 ...
- Unity3d之ScrollView实现图片浏览切换功能----折磨的学习
由于项目需要,需要用NGUi实现一个图片浏览切换的功能,于是参考官方NGUI例子的ScrollView做了一个例子,初始看上去基本实现了自己想要的功能. 但是测试后发现当隐藏其中一张图片后,后面图片不 ...