Skip to content

Commit c2a280c

Browse files
committed
[IMP] advanced_development: expand slides on method decorators
1 parent c34f79d commit c2a280c

File tree

1 file changed

+42
-14
lines changed

1 file changed

+42
-14
lines changed

advanced_development.html

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -162,21 +162,49 @@ <h2>Method decorators</h2>
162162
</code></pre>
163163
</section>
164164
<section>
165-
<h2>Other decorators</h2>
166-
<ul>
167-
<li>specify computed field dependencies</li>
168-
<pre><code class="python" data-trim>
169-
@api.depends('name')
170-
</code></pre>
171-
<li>declare onchange methods</li>
172-
<pre><code class="python" data-trim>
173-
@api.onchange('name')
174-
</code></pre>
175-
<li>declare constraint methods</li>
176-
<pre><code class="python" data-trim>
165+
<h2>Method decorators (2)</h2>
166+
<p>Specify return model:</p>
167+
<pre><code class="python" data-trim>
168+
@api.model
169+
@api.returns('res.partner')
170+
def get_default_partner(self):
171+
return self.env.ref('base.main_partner')
172+
173+
# new API call returns a recordset
174+
partner = records.get_default_partner()
175+
176+
# old API call returns a list of ids
177+
partner_ids = old_model.get_default_partner(cr, uid, context)
178+
</code></pre>
179+
</section>
180+
<section>
181+
<h2>Method decorators (3)</h2>
182+
<p>Declare constraint methods:</p>
183+
<pre><code class="python" data-trim>
177184
@api.constrains('name')
178-
</code></pre>
179-
</ul>
185+
def check_name(self):
186+
for record in self:
187+
if len(record.name) > 80:
188+
raise ValueError("Name is too long: %r" % record.name)
189+
</code></pre>
190+
<p>The method is expected to raise an exception when the
191+
constraint is not satisfied.</p>
192+
</section>
193+
<section>
194+
<h2>Method decorators (4)</h2>
195+
<p>Declare onchange methods:</p>
196+
<pre><code class="python" data-trim>
197+
@api.onchange('name')
198+
def onchange_name(self):
199+
if self.name:
200+
self.other_name = self.name.upper()
201+
if len(self.name or "") > 40:
202+
return {'warning': {
203+
'title': _("Warning"),
204+
'message': _("Name migh be a bit long"),
205+
}}
206+
</code></pre>
207+
<p>"self" is a pseudo-record that only lives in cache.</p>
180208
</section>
181209
<section>
182210
<h2>Computed Fields</h2>

0 commit comments

Comments
 (0)