[IMP] core: SQLObject replaces _sql_constraints
task-3390431 closes odoo/documentation#11071 Related: odoo/odoo#175783 Related: odoo/enterprise#68589 Signed-off-by: Krzysztof Magusiak (krma) <krma@odoo.com>
This commit is contained in:
parent
381ae7793b
commit
6395455f0b
@ -854,9 +854,10 @@ Symbols and Conventions
|
|||||||
at the beginning of the method.
|
at the beginning of the method.
|
||||||
|
|
||||||
- In a Model attribute order should be
|
- In a Model attribute order should be
|
||||||
#. Private attributes (``_name``, ``_description``, ``_inherit``, ``_sql_constraints``, ...)
|
#. Private attributes (``_name``, ``_description``, ``_inherit``, ...)
|
||||||
#. Default method and ``default_get``
|
#. Default method and ``default_get``
|
||||||
#. Field declarations
|
#. Field declarations
|
||||||
|
#. SQL constraints and indexes
|
||||||
#. Compute, inverse and search methods in the same order as field declaration
|
#. Compute, inverse and search methods in the same order as field declaration
|
||||||
#. Selection method (methods used to return computed values for selection fields)
|
#. Selection method (methods used to return computed values for selection fields)
|
||||||
#. Constrains methods (``@api.constrains``) and onchange methods (``@api.onchange``)
|
#. Constrains methods (``@api.constrains``) and onchange methods (``@api.onchange``)
|
||||||
|
@ -63,8 +63,6 @@ Odoo automatically exports translatable strings from "data"-type content:
|
|||||||
* if ``selection`` is present and a list (or tuple), it's exported
|
* if ``selection`` is present and a list (or tuple), it's exported
|
||||||
* if their ``translate`` attribute is set to ``True``, all of their existing
|
* if their ``translate`` attribute is set to ``True``, all of their existing
|
||||||
values (across all records) are exported
|
values (across all records) are exported
|
||||||
* help/error messages of :attr:`~odoo.models.Model._constraints` and
|
|
||||||
:attr:`~odoo.models.Model._sql_constraints` are exported
|
|
||||||
|
|
||||||
Explicit exports
|
Explicit exports
|
||||||
================
|
================
|
||||||
|
@ -63,7 +63,6 @@ value::
|
|||||||
Defaults to whatever value was set for :attr:`~._auto`.
|
Defaults to whatever value was set for :attr:`~._auto`.
|
||||||
|
|
||||||
.. autoattribute:: _table
|
.. autoattribute:: _table
|
||||||
.. autoattribute:: _sql_constraints
|
|
||||||
|
|
||||||
.. autoattribute:: _register
|
.. autoattribute:: _register
|
||||||
.. autoattribute:: _abstract
|
.. autoattribute:: _abstract
|
||||||
@ -509,6 +508,30 @@ behavior is desired:
|
|||||||
:class:`~odoo.fields.Many2one`
|
:class:`~odoo.fields.Many2one`
|
||||||
:type: :class:`~odoo.addons.base.models.res_company`
|
:type: :class:`~odoo.addons.base.models.res_company`
|
||||||
|
|
||||||
|
Constraints and indexes
|
||||||
|
=======================
|
||||||
|
|
||||||
|
Similarly to fields, you can declare
|
||||||
|
:attr:`~odoo.models.Constraint`,
|
||||||
|
:attr:`~odoo.models.Index` and :attr:`~odoo.models.UniqueIndex`.
|
||||||
|
The name of the attribute must begin with `_` to avoid name clashes with field
|
||||||
|
names.
|
||||||
|
|
||||||
|
You can customize error messages.
|
||||||
|
They can either be strings and their translation will be provided in the internal
|
||||||
|
reflected constraint table.
|
||||||
|
Otherwise, they can be functions that take `(env, diag)` as parameters
|
||||||
|
which respectively denote the environment and psycopg diagnostics.
|
||||||
|
|
||||||
|
.. example::
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
class AModel(models.Model):
|
||||||
|
_name = 'a.model'
|
||||||
|
_my_check = models.Constraint("CHECK (x > y)", "x > y is not true")
|
||||||
|
_name_idx = models.Index("(last_name, first_name)")
|
||||||
|
|
||||||
Recordsets
|
Recordsets
|
||||||
==========
|
==========
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ Changelog
|
|||||||
Odoo Online version 18.1
|
Odoo Online version 18.1
|
||||||
========================
|
========================
|
||||||
|
|
||||||
|
- Declare constraints and indexes as model attributes with `#175783 <https://github.com/odoo/odoo/pull/175783>`_.
|
||||||
- The `json` controllers have been renamed to `jsonrpc`. They are called the same, only the
|
- The `json` controllers have been renamed to `jsonrpc`. They are called the same, only the
|
||||||
`type` in the python files changed. See `#183636 <https://github.com/odoo/odoo/pull/183636>`_.
|
`type` in the python files changed. See `#183636 <https://github.com/odoo/odoo/pull/183636>`_.
|
||||||
|
|
||||||
|
@ -797,7 +797,9 @@ Model constraints
|
|||||||
|
|
||||||
Odoo provides two ways to set up automatically verified invariants:
|
Odoo provides two ways to set up automatically verified invariants:
|
||||||
:func:`Python constraints <odoo.api.constrains>` and
|
:func:`Python constraints <odoo.api.constrains>` and
|
||||||
:attr:`SQL constraints <odoo.models.Model._sql_constraints>`.
|
:attr:`SQL constraints <odoo.models.Constraint>`.
|
||||||
|
In a similar way, you can add more complex
|
||||||
|
:attr:`SQL indexes <odoo.models.Index>`.
|
||||||
|
|
||||||
A Python constraint is defined as a method decorated with
|
A Python constraint is defined as a method decorated with
|
||||||
:func:`~odoo.api.constrains`, and invoked on a recordset. The decorator
|
:func:`~odoo.api.constrains`, and invoked on a recordset. The decorator
|
||||||
@ -819,11 +821,9 @@ raise an exception if its invariant is not satisfied::
|
|||||||
Add a constraint that checks that the instructor is not present in the
|
Add a constraint that checks that the instructor is not present in the
|
||||||
attendees of his/her own session.
|
attendees of his/her own session.
|
||||||
|
|
||||||
SQL constraints are defined through the model attribute
|
Constraints and indexes are defined using:
|
||||||
:attr:`~odoo.models.Model._sql_constraints`. The latter is assigned to a list
|
:attr:`~odoo.models.Constraint`,
|
||||||
of triples of strings ``(name, sql_definition, message)``, where ``name`` is a
|
:attr:`~odoo.models.Index` and :attr:`~odoo.models.UniqueIndex`.
|
||||||
valid SQL constraint name, ``sql_definition`` is a table_constraint_ expression,
|
|
||||||
and ``message`` is the error message.
|
|
||||||
|
|
||||||
.. exercise:: Add SQL constraints
|
.. exercise:: Add SQL constraints
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ users from setting a negative expected price.
|
|||||||
|
|
||||||
Odoo provides two ways to set up automatically verified invariants:
|
Odoo provides two ways to set up automatically verified invariants:
|
||||||
:func:`Python constraints <odoo.api.constrains>` and
|
:func:`Python constraints <odoo.api.constrains>` and
|
||||||
:attr:`SQL constraints <odoo.models.Model._sql_constraints>`.
|
:attr:`SQL constraints <odoo.models.Constraint>`.
|
||||||
|
|
||||||
SQL
|
SQL
|
||||||
===
|
===
|
||||||
@ -33,14 +33,16 @@ SQL
|
|||||||
:align: center
|
:align: center
|
||||||
:alt: Constraints on names
|
:alt: Constraints on names
|
||||||
|
|
||||||
SQL constraints are defined through the model attribute
|
SQL objects are defined through :attr:`~odoo.models.Constraint` attributes.
|
||||||
:attr:`~odoo.models.Model._sql_constraints`. This attribute is assigned a list
|
|
||||||
of triples containing strings ``(name, sql_definition, message)``, where ``name`` is a
|
|
||||||
valid SQL constraint name, ``sql_definition`` is a table_constraint_ expression
|
|
||||||
and ``message`` is the error message.
|
|
||||||
|
|
||||||
You can find a simple example
|
Simple example:
|
||||||
`here <https://github.com/odoo/odoo/blob/24b0b6f07f65b6151d1d06150e376320a44fd20a/addons/analytic/models/analytic_account.py#L20-L23>`__.
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
_check_percentage = models.Constraint(
|
||||||
|
'CHECK(percentage >= 0 AND percentage <= 100)',
|
||||||
|
'The percentage of an analytic distribution should be between 0 and 100.',
|
||||||
|
)
|
||||||
|
|
||||||
.. exercise:: Add SQL constraints.
|
.. exercise:: Add SQL constraints.
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user