Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

How to check _sql_constraints using function: Odoo 9

By
shameem@aχoncoms
on 11/4/16, 8:02 AM 194 views

Hi guys,

I want to check _sql_constraints of  a model using function

Example , I have a function

@api.one
def check_constraint(self)
     # CODE TO CHECK
     return True # If ok

     return False # If not ok

The below constraint must raise an error if the function returns False

_sql_constraints = [ .................]

I know It is possible  by overriding the create method. But I forgot the correct way

Thanks in advance

 

1

Axel Mendoza

--Axel Mendoza--
10228
| 7 7 8
Camaguey, Cuba
--Axel Mendoza--

DevOps - Full stack - Software Architect - Developer - Technology Integrator

I could help you to develop anything and solve complex problems based on technologies, integrations and tricky stuffs mostly in Python with OpenERP/Odoo, Zato, Django and many others frameworks programming languages and technologies.

I offers consulting services to anyone with an unanswered questions or needs for customizations. Think about it, maybe it's better to have an expert to solve your issues and projects than having a full time employee trying to understand what to do an how

Reach me at aekroft@gmail.com

Axel Mendoza
On 11/4/16, 7:08 PM

Use python constraints instead

https://www.odoo.com/documentation/9.0/reference/orm.html#openerp.api.constrains

https://www.odoo.com/documentation/9.0/reference/orm.html#openerp.models.Model._constraints

Thanks Axel , It is working

shameem@aχoncoms
on 11/5/16, 7:03 AM
5

Nilmar Shereef

--Nilmar Shereef--
811
| 4 3 7
Calicut, India
--Nilmar Shereef--

Python Developer. Android Developer. .Net Developer

Nilmar Shereef
On 11/4/16, 11:46 PM

Hi Shameem,

you have two ways.

one way:

std_id = fields.Char(string="Student ID", required=True)   

_sql_constraints = [('std_id_uniq', 'unique(std_id)', 'This ID already exists !')]


Another way using api:

mark1 = fields.Integer(string="Total Mark")

@api.constrains('mark1')   

def _check_mark1(self):
        if self.mark1 == 0:
            raise ValidationError("Please enter the marks !")


Also you can check any logical operations inside the constrains function.

Hope this may help you


Thanks Nilmar,

It is working while adding @api.one

Same answer of Axel , you explained more

You have an upvote

shameem@aχoncoms
on 11/5/16, 7:05 AM

Good. Keep going :)

Nilmar Shereef
on 11/5/16, 7:07 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

0 follower(s)

Stats

Asked: 11/4/16, 8:02 AM
Seen: 194 times
Last updated: 11/5/16, 7:02 AM