Constraints in odoo 14

CONSTRAINTS IN ODOO

As we know that Odoo allows you to define constraints on the field(s). Constraints gives you control over the data as you wish. If a user attempts to store data in a field that violates a constraint then, an error should be raised. Models can have validation that prevents them from entering unacceptable conditions.

Odoo provides two ways to set up automatically verified invariants:

  • Python constraints & SQL Constraints

Python Cconstraints:

  1. Create a new class in models then add this code.

eg:class <ClassName>(models.Model):

_name=’class.name’

field_name= fields.Type(“String”)

@api.constrains(‘name’, ‘description’)

                  def _check_description(self):

                 for record in self: if record.name == record.description:

Fields name and description must be different.

2. Create corresponding xml file.

eg:<?xml version=”1.0″ encoding=”UTF-8″ ?>

<odoo>

<template id=”report_header_template_id“>

<t t-call=”web.html_container”>

<t t-if=”not o” t-set=”o” t-value=”doc”/>

<t t-if=”not company”> <!– Multicompany –>

<t t-if=”company_id”>

<t t-set=”company” t-value=”company_id”/>

</t>

<t t-elif=”o and ‘company_id’ in o”>

<t t-set=”company” t-value=”o.company_id.sudo()”/>

</t>

<t t-else=”else”> <t t-set=”company” t-value=”res_company”/>

</t>

</t>

<div class=”header” t-att-style=”report_header_style”>

<div class=”row”>

</div>

</div>

<div class="col-9 text-right" t-field="company.report_header" name="moto">
                <div t-field="company.partner_id" t-options="{&quot;widget&quot;: &quot;contact&quot;, &quot;fields&quot;: [&quot;address&quot;, &quot;name&quot;], &quot;no_marker&quot;: true}"/>
            </div>
            <div class="article" t-att-data-oe-model="o and o._name" t-att-data-oe-id="o and o.id" t-att-data-oe-lang="o and o.env.context.get('lang')">
                <t t-raw="0"/>
</div>
            <div class="footer o_background_footer">
                <div>
                    <div t-field="company.report_footer"/>
                    <div t-if="report_type == 'pdf'" class="text-muted">
                        <hr style="width:100%;" color="red"/>
                        <div class="row">
                            <div class="col-6" style="text-align:right;">
                                Page:
                               <span class="page"/>
                                /
                                <span class="topage"/>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </t>
    </template>
<template id="report_body_template_id">
    <t t-call="web.html_container">
        <t t-foreach="docs" t-as="o">
            <t t-call="module_name.report header template id">
                <div class="page">

</div>

</t>

</t>

</t>

</template>

</odoo>
> here you have to change header template_id, body template_id and under body template_id change this one also <t t-call=”module_name.header_template_id”>

 

  • Mandatory to add xml file in __manifest__.xml

 

To know more about us

Leave a Reply

Your email address will not be published.