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 override field-specific saving behaviour

By
Hannes Smit
on 7/29/13, 12:00 PM 1,611 views

In some cases you might want to manage which values get written into the database.

Some examples: Say, I want to save a phone number which always must be in the format 'XX-XXXXXXXX' Or a zip code in the format 'XXXX XX' Or you just may want to strip certain characters like trailing spaces.

The given examples are typically 'char' fields.

Which methods or __init__ parameters of these fields can I use to hook in a sort of pre-processing method?

So I expect something like this:

def preprocess(self, cr, uid, id, name, value, context=None):
    value = value.strip()
    super(my_field, self).preprocess(cr, uid, id, name, value, context=context)
...
... in _columns:

    'column1': fields.char('Phone', size=11, preprocess=preprocess),
}

Another option I thought of was making a function field with its own fnct and fnct_inv, and with a store=True. But I'm not sure how to make this fnct method in such a way that it behaves like a normal field.

Note: I'm interested in field-specific callbacks, not in write() and create() overrides.

3

Grover Menacho

--Grover Menacho--
2523
| 5 7 8
La Paz, Bolivia
--Grover Menacho--

Systems Engineer + MBA

Grover Menacho
On 7/29/13, 1:20 PM

I'm not sure if I am understanding your question, anyways you can override write or create functions.

Here you can check and modify the values that are received before storing them.

def write(self, cr, uid, ids, values, context=None):
#Check and modify what you want on values
    return super(name_class, self).write(cr, uid, ids, values, context=context)

def create(self, cr, uid, values, context=None):
#Check and modify what you want on values
    return super(name_class, self).create(cr, uid, values, context=context)

I know that. But I want a field-specific callback. (Look at the base class of fields.char and see a 'set()' method. Something like that, if that is the right place...

Hannes Smit
on 7/30/13, 5:13 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: 7/29/13, 12:00 PM
Seen: 1611 times
Last updated: 3/16/15, 8:10 AM