Skip to Content
Menu
This question has been flagged
2 Replies
9542 Views

I want to create custom field type with use of existing field type.Just like Odoo have field types: Char,Integer etc.I want create new Interger type with use of existind Integer field type. So new custom field type has existing and new functionlity.

Odoo allow create custom field type? If yes then how to create custom field type?

I guess that inherit existing field type we can create custom field type just like we create custom module with use of inherit existing model.

Avatar
Discard
Best Answer

Hi Girish,

Yes you can. You would need to create a new Class in order to create your own view type. Here is an example of how the integer field type is made in Odoo:

class Integer(Field):
    type = 'integer'
    column_type = ('int4', 'int4')
    _slots = {
        'group_operator': 'sum',
    }

    def convert_to_column(self, value, record, values=None, validate=True):
        return int(value or 0)

    def convert_to_cache(self, value, record, validate=True):
        if isinstance(value, dict):
            # special case, when an integer field is used as inverse for a one2many
            return value.get('id', False)
        return int(value or 0)

    def convert_to_read(self, value, record, use_name_get=True):
        # Integer values greater than 2^31-1 are not supported in pure XMLRPC,
        # so we have to pass them as floats :-(
        if value and value > MAXINT:
            return float(value)
        return value

    def _update(self, records, value):
        # special case, when an integer field is used as inverse for a one2many
        cache = records.env.cache
        for record in records:
            cache.set(record, self, value.id or 0)

    def convert_to_export(self, value, record):
        if value or value == 0:
            return value if record._context.get('export_raw_data') else ustr(value)
        return ''
You can see all the current field types and how they're made in the fields.py file. See https://github.com/odoo/odoo/blob/4687faefbfb62ccb4878fb29a77015874f7aed90/odoo/fields.py#L1161-L1193

After you've built the class and the field type you'll need to create your own field widgets and logic to convert values to the output you'd like. 
Make sure you test your own field type thoroughly though as you're in the core of both Odoo and postgreSQL. You'll need to test a lot of cases before putting this into production.

Regards,
Yenthe

Avatar
Discard
Author

"create your own field widgets and logic to convert values to the output you'd like" any hint about this. I mean where to found the related code or where to bind code about the field widget

Author

Hi Yenthe Van Ginneken, Thanks for the help. I created a custom filed type and widget also and it's working fine. One more help required for how to show custom field type in Odoo studio view?

Best Answer

Yes, why not? You can do anything you like in Odoo, although it requires certain tech skills.

To start with: open the core file odoo/fields.py and have a look how fields are defined. You may inherit one of those. 

Then, have a look at the module web and its javascript. Here JavScript widget to picture fields are defined.


Avatar
Discard
Related Posts Replies Views Activity
0
Dec 19
1828
4
Dec 19
5473
2
Mar 18
7601
0
Mar 18
3187
1
Jan 18
4178