Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
Coopdevs OCB mirror
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
External wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Coopdevs
Odoo
Coopdevs OCB mirror
Commits
0c204037
Commit
0c204037
authored
10 years ago
by
Raphael Collet
Browse files
Options
Downloads
Patches
Plain Diff
[IMP] api: improve documentation of decorators constrains, depends, onchange
parent
b41599e1
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
openerp/api.py
+32
-3
32 additions, 3 deletions
openerp/api.py
with
32 additions
and
3 deletions
openerp/api.py
+
32
−
3
View file @
0c204037
...
...
@@ -138,14 +138,33 @@ def propagate(from_method, to_method):
def
constrains
(
*
args
):
"""
Return a decorator that specifies the field dependencies of a method
implementing a constraint checker. Each argument must be a field name.
implementing a constraint checker. Each argument must be a field name::
@api.one
@api.constrains(
'
name
'
,
'
description
'
)
def _check_description(self):
if self.name == self.description:
raise ValueError(
"
Fields name and description must be different
"
)
The method is invoked on the records where one of the given fields has
been modified. It is expected to raise an exception when the constraint
is not satisfied.
"""
return
lambda
method
:
decorate
(
method
,
'
_constrains
'
,
args
)
def
onchange
(
*
args
):
"""
Return a decorator to decorate an onchange method for given fields.
Each argument must be a field name.
Each argument must be a field name::
@api.onchange(
'
partner_id
'
)
def _onchange_partner(self):
self.message =
"
Dear %s
"
% (self.partner_id.name or
""
)
In the form views where the field appears, the method will be called
when one of the given fields is modified. The method is invoked on a
pseudo-record that contains the values present in the form. Field
assignments on that record are automatically sent back to the client.
"""
return
lambda
method
:
decorate
(
method
,
'
_onchange
'
,
args
)
...
...
@@ -153,7 +172,17 @@ def onchange(*args):
def
depends
(
*
args
):
"""
Return a decorator that specifies the field dependencies of a
"
compute
"
method (for new-style function fields). Each argument must be a string
that consists in a dot-separated sequence of field names.
that consists in a dot-separated sequence of field names::
pname = fields.Char(compute=
'
_compute_pname
'
)
@api.one
@api.depends(
'
partner_id.name
'
,
'
partner_id.is_company
'
)
def _compute_pname(self):
if self.partner_id.is_company:
self.pname = (self.partner_id.name or
""
).upper()
else:
self.pname = self.partner_id.name
One may also pass a single function as argument. In that case, the
dependencies are given by calling the function with the field
'
s model.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment