- 上传者: Administrator
- 上传时间:2023年11月14日 03时24分57秒
* Model Methods and Decorators ** *** The class in Odoo models contains field declarations as well as business logic methods. The method defined for ......
Model Methods and Decorators
The class in Odoo models contains field declarations as well as business logic methods. The method defined for a button click is one example of a simple type of method.
For example, clicking the confirm button in a form view changes the state from 'Draft' to 'Confirmed.'
The function will first be declared from the XML:
button name="action_confirm" string="Confirm" type="object"/
The form view will include a 'Confirm' button, whose python action is specified in the button's name attribute (action confirm).
state=fields.Selection([(draft,'Draft'),(confirmed,'Confirmed')],default='draft')
def action_confirm(self):
self.state = 'confirmed'
In this case, action confirm is a Python method with self as an argument.'self,' the first argument method of Odoo models, can take additional arguments.
Decorators.
Decorators allow you to change the behavior of a method. It simply adds functionality to a function before returning it.
@api.autovaccum
The daily vacuum cron job calls the methods decorated by this decorator (model ir.autovacuum). It is used for tasks that do not require a specific cron job.
@api.constrains(*args)
Fields from the particular model are the arguments given to the decorator. When any of the fields are altered, the function will be called.
@constrains only support field names, not dotted fields(related fields, Eg:partner_id.city).
@constrains will only be executed if the defined fields in the decorated method are used in the model's create or write function.
It implies that fields won't cause the function to be called during record generation if they don't exist in the view.
An override of create is required to ensure that a constraint is always active.
@api.depends
This decorator can be used to declare the field dependencies of the "compute" method.Each argument must be a string made up of a dot-separated list of field names:
pname = fields.Char(compute='_compute_pname')
@api.depends('partner_id.name', 'partner_id.is_company')
def _compute_pname(self):
for record in self:
if record.partner_id.is_company:
record.pname = (record.partner_id.name or "").upper()
else:
record.pname = record.partner_id.name
@api.depends_context(*args)
This decorator can be used to declare the context dependencies of a non-stored "compute" method. Each passed argument is a key in the context dictionary.
price = fields.Float(compute='_compute_product_price')
@api.depends_context('pricelist')
def _compute_product_price(self):
for product in self:
if product.env.context.get('pricelist'):
pricelist = self.env['product.pricelist'].browse(product.env.context['pricelist'])
else:
pricelist = self.env['product.pricelist'].get_default_pricelist()
product.price = pricelist.get_products_price(product).get(product.id, 0.0)
@api.model
Decorate a record-style method in which self is a recordset but the contents of its are irrelevant; only its model is.
@api.model
def method(self, args):
//Add your method here
@api.model_create_multi
Decorate a method with a list of dictionaries if it creates several records. It is referred to as a list of dictionaries.
record = model.create(vals)
records = model.create([vals, ...])
@api.onchange(*args)
The view's fields will be passed as arguments to the 'onchange' decorator. The method will be called when the view's field is modified. The method is called on a pseudo-record that contains the form's values. The field assignments on that record are automatically returned to the client.
@api.onchange('partner_id')
def _onchange_partner(self):
self.message = "Dear %s" % (self.partner_id.name or "")
return {
'warning': {'title': "Warning", 'message': "What is this?",'type': 'notification'},}
@onchange only supports simple field names, relational fields are not supported(partner_id.city)
Because @onchange returns a recordset of pseudo-records, trying to call any of the CRUD methods (create(), read(), write(), unlink()) on this recordset results in undefined behavior, because the records may not yet exist in the database.
Instead, we could use the update() method or easily set the field as in this example.
Onchange cannot be used to modify a one2many or many2many field.
@api.ondelete(*, at_uninstall)
Mark a method to be executed during unlink().
The methods decorated with @ondelete should raise an error if certain conditions are met, as well as the method should be named either _unlink_if_<condition> or _unlink_except_ <not_condition>.
@api.ondelete(at_uninstall=False)
def _unlink_if_user_inactive(self):
if any(user.active for user in self):
raise UserError("Can't delete an active user!")
# same as above but with _unlink_except_* as method name
@api.ondelete(at_uninstall=False)
def _unlink_except_active_user(self):
if any(user.active for user in self):
raise UserError("Can't delete an active user!")
Parameters:
at_uninstall (bool) – When the module that implements this function is uninstalled, this decorated method will be called. It should nearly always be False to prevent those errors from occurring upon module uninstalling.
@api.returns(model, downgrade=None, upgrade=None)
For methods that return model instances return a decorator.
Parameters
· model – For the current model, a model name or'self'
· downgrade – The fuction, downgrade(self, value, *args, **kwargs) that converts a record-style value to the traditional-style output.
· upgrade – A function upgrade(self, value, *args, **kwargs) is used to convert a traditional-style value to a record-style output.
The parameters self, *args, and **kwargs are passed to the method in the record style.
The decorator modifies the method's output to be in the api style: id, ids, or False for the traditional form style and the recordset for the record style.
@model
@returns('res.partner')
def find_partner(self, arg):
# return some record
# output depends on call style: traditional vs record style
partner_id = model.find_partner(cr, uid, arg, context=context)
# recs = model.browse(cr, uid, ids, context)
partner_record = recs.find_partner(arg)
It should be noted that the decorated method must satisfy to that convention.
Those decorators are automatically inherited: A method that overrides an existing decorated method will be decorated in the same way.@returns(model).
本文章从网上收集,如有侵权请联系tderp@tderp.com删除
- 微信扫一扫,一分也是爱:
服务原则及地区范围
宜兴通达网络科技有限公司,地处中国宜兴环科园内,是一家高新技术企业。公司在企业网络维护和企业信息化建设与咨询方面,有10多年经验。
我公司愿与客户一道,力求彻底解决客户问题!
我们不是在给企业提供“头痛医头、脚痛医脚”的暂时解决方案,而是在部署根本性安全与稳定服务!!
我们愿携手客户,建立企业IT规划;杜绝随意安装系统、软件等操作;力求共同维护有序、安全、稳定的网络办公环境!!!
IT服务,服务是根本,客户是上帝;我们提供快速响应、快速上门、快速排查,提供优质高效的服务!!!!
通达科技提供全国范围内的服务,服务形式包括远程协助、电话咨询、电子邮件咨询、传真咨询、问答平台的问题解决等。
宜兴地区提供上门服务:
- 市区服务:宜城街道、城北街道(屺亭街道)、新街街道、新庄街道、环科园、渚桥开发区
- 市郊服务:张渚镇、西渚镇、太华镇、徐舍镇、官林镇、杨巷镇、新建镇、和桥镇、高塍镇、万石镇、周铁镇、芳桥镇、丁蜀镇、湖父镇。