I have tried unique in sql_constaints in OpenERP(Odoo) using two different methods using flower brackets {} or square brackets []. Both works fine. Which one is correct?
_sql_constraints = {
('email_uniq', 'unique(email)', ' Please enter Unique Email id.')
}
(or)
_sql_constraints = [
('email_uniq', 'unique(email)', ' Please enter Unique Email id.')
]
P.S: But if I want to use more than a constraint it accepting only square brackets [] like this example.
_sql_constraints = [
('email_uniq', 'unique(email)', ' Please enter Unique Email id.'),
('contact_uniq', 'unique(contact)', ' Please enter Unique Mobile no.')
]
What is the reason behind it?
The correct one is square brackets syntax.
you can grep on _sql_constraints
and see it is what is always used,
in the ORM code in openerp/models.py
we can see that the default value is an empty list:
_sql_constraints = []
#...
cls._local_sql_constraints = cls.__dict__.get('_sql_constraints', [])
list of
(name, sql_definition, message)
triples defining SQL constraints to execute when generating the backing table.
In python2 you can get a list with the syntax []
.
The syntax {}
either creates:
{}
or if there is keys values like this: {'keyA': 'valueA', 'keyB': 'valueB'}
,{'value1', 'valueB', 42}
In odoo the syntax for sql constraint is list of (name, sql_definition, message)
. for example
_sql_constraints = [
('name_uniq', 'unique(name)', 'Custom Warning Message'),
('contact_uniq', 'unique(contact)', 'Custom Warning Message')
]
you can give more then one sql constraint in list of tuples.
when you define a dictionary with out key & value python treat it as set. and set can be itrable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With