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.
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?
if 0 < self.comp_min < self.comp_max:
self.comp_min = None # this is not ideal, as value before modification should be here.
if not self._check_comp_min():
'message': 'You have entered wrong value for component'
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...
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
|Asked: 3/9/16, 11:32 AM|
|Seen: 457 times|
|Last updated: 3/14/16, 6:34 AM|