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

Attribute condition for field to limit possible values?

By
Andrius Laukavičius
on 6/5/14, 8:17 AM 1,804 views

Is there some good simple way to limit what values can be entered in specific field?

For example I want that in my field you could only enter positive values. I could do that by overriding that models create and write methods and checking if that specific field value is positive. If not I would throw exception. But maybe there is some simpler better way like it is with required attribute or similar?

For exmaple is there some similar way like:

'my_field': fieds.integer('My field' limit=[('val', '>=', 0)])

 

P.S. How can I change my text format look like code format in this new odoo forum?..

2

René Schuster

--René Schuster--
1560
| 5 5 8
Weinheim, Germany
--René Schuster--

Challenge Everything!

René Schuster
On 6/5/14, 8:32 AM

Two ways I can think of:

  • 1. Use custom constraints:

def _check_my_field(self, cr, uid, ids, context=None):
    for obj in self.browse(cr, uid, ids, context=context):
        if obj.my_field < 0:
        return False;
    return True;

_constraints = [
    (_check_my_field, 'ErrorMessage', ['my_field'])
]

 

  • 2. Use on_change method:

def onchange_my_field(self, cr, uid, ids, field_value):
    result = {};
    if field_value < 0:
        result = {'value': {'my_field': 0}}
    return result;

<field name="my_field" on_change="onchange_my_field(my_field)"/>

 

Best regards.

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: 6/5/14, 8:17 AM
Seen: 1804 times
Last updated: 3/16/15, 8:10 AM