This question has been flagged
2 Replies
9167 Views

hello i am new in odoo 


i wnat to know about all decoraters i alrady seen all documantation but its very hard to understand for me .

my question is

when i use @api.model?

when i use @api.dependace

 when i use @api.onchange


please ans in easy way to better understand for me


thanks in advance



Avatar
Discard
Author

thank you Waleed Mohsen

can u give me information about all of the api in odoo 15
it is possible for you?

Best Answer

Hi,

api.model

The model decorator is used with the methods where self is a record set, but only the model is relevant and not its contents. It helps in the code migration since it will convert the old API calls to the new ones, which makes the migration process faster.

Here is an example code for @api.model:

@api.model
def _get_default_currency(self):
''' Get the default currency from either the journal, either the default journal's company. '''
journal = self._get_default_journal()
return journal.currency_id or journal.company_id.currency_id

api.depends

The depends decorator is used to specify the compute dependencies for a computing method. Each argument must be a string of field names, where it can be a simple or a dot-separated sequence of field names. It is possible to pass a single function as an argument, where the dependencies are given by calling the function with the field’s model.

Here is an example code for @api.depends:

complete_name = fields.Char('Complete Name', compute='_compute_complete_name', store=True)
@api.depends('name', 'parent_id.complete_name')
def _compute_complete_name(self):
for department in self:
if department.parent_id:
department.complete_name = '%s / %s' % (
department.parent_id.complete_name, department.name)
else:
department.complete_name = department.name

api.onchange

The onchange method decorator is mostly used with the onchange method for the given fields. The onchange method will be triggered while we change the value of any of the fields specified with the onchange decorator from the form view. The onchange method will be invoked on a pseudo-record that contains all the values of fields present in the form view. Even though we cannot perform the CRUD operations in that record, the field assignments are updated in the form view. The onchange decorator does not support the dotted names(fields of relational fields like product_id.default_code), and those will be ignored. We can only use simple field names.

Here is an example code for @api.onchange:

@api.onchange('analytic_account_id', 'analytic_tag_ids')
def _onchange_mark_recompute_taxes_analytic(self):
''' Trigger tax recomputation only when some taxes with analytics
'''
for line in self:
if not line.tax_repartition_line_id and any(tax.analytic for tax in line.tax_ids):
line.recompute_tax_line = True

Regards

Avatar
Discard
Best Answer

@api.model decorator used on methods for which only the model is important, not the contents of the recordset. so self should be used as a reference for the model, without expecting it to contain actual records.
In another words it's used when you need to do something with model itself and don't need to modify/check some exact model's record/records

@api.depends(fld1,...) is used for computed field functions, to identify on what changes the (re)calculation should be triggered. It must set values on the computed fields, otherwise it will error.

@api.onchange(fld1,...) is used in the user interface, to automatically change some field values when other fields are changed. The self argument is a singleton with the current form data, and the method should set values on it for the changes that should happen in the form. It doesn’t actually write to database records, instead it provides information to change the data in the UI form.

Avatar
Discard

What if I don't declare @api.model?
What will happen?
I try to remove it but there is no effect after it.