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

Validation defined on model fields level?

By
Richard Kellner
on 3/9/16, 11:32 AM 453 views

Is there a way how to validate on a model level? Let say I have integer field:

comp_min = fields.Integer(string='Min. component item')
comp_max = fields.Integer(string='Max. component item')

How do I set that comp_min have to be greater than 0 and lower than comp_max at the same time?

I know I can set min in template, but it would make sence to define this on model level, other wise I have to add check in template in model on change and possibly other places so I am sure that I do not have mismatched data.

1

Axel Mendoza

--Axel Mendoza--
10205
| 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 3/9/16, 3:29 PM

You could do it using a python constraint, like:

@api.model

def _check_comp_min(self):

obj = self[0]

if 0 < obj.comp_min < obj.comp_max:

return True

return False

_constraints = [

(_check_comp_min,'The comp_min field is invalid',['comp_min'])

]

Hi Axel ,you are helping all the beginners and expert by odoo blogs.your answers are perfect and correct .awesome job ...Thanks for all answers quickly .I think you are working as 24/7 time it's marvellous...keep in touch ,Keep it up bye cya

Odoo
on 3/14/16, 10:29 AM

Thanks for your kind words. I appreciate that. Yes, I reviewing closely every question that I could help with my previous experience and try to learn about others errors while solving it and also push me to see code and modules that I never used before. This bring an awesome experience because I'm not dealing only with my daily Odoo situations. Always that the community appreciate my contributions is a good day. Beginners today could be Odoo experts in the future

Axel Mendoza
on 3/14/16, 3:36 PM
0
Richard Kellner
On 3/14/16, 6:34 AM

Unfortunately I cant comment on @Axel answer.

According documentation _constraints is deprecated and I shall use constraints decorator, so the right way in odoo 9.x would be something like this?


def _check_comp_min(self):

if 0 < self.comp_min < self.comp_max:

return True

     self.comp_min = None # this is not ideal, as value before modification should be here.

return False


@api.onchange('comp_max', 'comp_min'):

def validation_comp_min(self):

     if not self._check_comp_min():

         return {

            'warning': {

                 'title': 'Error',

                 'message': 'You have entered wrong value for component'

             }

         }

     return True


@api.constrains('comp_max', 'comp_min'):

def validation_comp_max(self):

     validation_result = self._check_comp_min()

     if validation_result != True:

         raise ValidationError('You have entered wrong value for component')

This way I would have just one validation function, but it have to be called twice for onchange (to validate immediatelly) and also on constraint so it is validated when saving. Or is there something similar like Django models have? Seems too much code for simple validation to be executed on change and constraint as well...

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

1 follower(s)

Stats

Asked: 3/9/16, 11:32 AM
Seen: 453 times
Last updated: 3/14/16, 6:34 AM