Skip to Content
Menu
This question has been flagged
1 Reply
3352 Views

Scenario:

  • Model with many fields with similar properties.

  • Export data to XML, the data needs to be processed to add a specific character to complete a fixed size. 
        Age: 25 = <age>025</age> 
        Length: 1,3 = <lenght>000130</length>


I need to extend some types of fields. I have already checked how to create new types of fields: 
https://www.odoo.com/es_ES/forum/ayuda-1/question/create-new-custom-field-type-odoo-145249

But I don't need new types, what I really need is to extend the current types to add information and set some properties instead of repeating the same properties for each field. I have tried to do it by inheritance: 

from odoo.fields import Selection, Char, Integer, Float, Text, Html
class SelectionYesNo(Selection):
  selection_yesno = [
                ('yes', 'Yes'),
                ('no', 'No')
              ]
  xml_size = 5
  def __init__(self, string='YesNo', **kwargs):
    super(SelectionYesNo, self).__init__(selection=self.selection_yesno,
                                         string=string,
                                         **kwargs)
class SelectionGender(Selection):
  selection_gender = [
                ('M', 'Male'),
                ('F', 'Female'),
                ('X', 'Other')
              ]
  xml_size = 1
  def __init__(self, selection=selection_gender, string='Gender', **kwargs):
    super(SelectionGender, self).__init__(selection=selection,
                                          string=string,
                                          **kwargs)


class CharName(Char): xml_size = 70 def __init__(self, string='Name', **kwargs): kwargs['size'] = 70 super(CharName, self).__init__(string=string, **kwargs)
class IntegerAge(Integer): xml_size = 3 def __init__(self, string='Age', **kwargs): super().__init__(string, **kwargs)
The Model:
from odoo import models, fields, api
from ..helpers import foobar_helper_field
class FooBar(models.Model): 
_name = 'foobar.foobar' name = fields.Char() name_custom = foobar_helper_field.CharName(string='Custom Name')
age = fields.Integer() age_custom = foobar_helper_field.IntegerAge(string='Custom Age')
length = fields.Float() length_custom = foobar_helper_field.FloatLength(string='Custom Float')
question = fields.Selection(selection=[('yes', 'Yes'), ('no', 'No')], string='Question') question_custom = foobar_helper_field.SelectionYesNo(string='Custom Question')
gender = fields.Selection(selection=[('male', 'Male'), ('female', 'Female'), ('other', 'Other')], string='Gender') gender_custom = foobar_helper_field.SelectionGender(string='Custom Gender')


In this way it works, I can access the new property xml_size and the properties are assigned, the problem is that the view does not show the tag with the string parameter that is passed when specifying the field in the model, but instead assigns the tag with the default value of the constructor 

https://ibb.co/J3YzYfJ
https://ibb.co/QknfhfL
https://ibb.co/56NRmXH
https://ibb.co/xj1zjyk

The string parameter must have a default value or an exception is thrown:

2019-12-15 14:05:13,491 17591 ERROR testdb odoo.modules.registry: Failed to load registry 
Traceback (most recent call last):
File "/home/odoo/odoo/odoo/modules/registry.py", line 86, in new
odoo.modules.load_modules(registry._db, force_demo, status, update_module)
File "/home/odoo/odoo/odoo/modules/loading.py", line 417, in load_modules
force, status, report, loaded_modules, update_module, models_to_check)
File "/home/odoo/odoo/odoo/modules/loading.py", line 313, in load_marked_modules
perform_checks=perform_checks, models_to_check=models_to_check
File "/home/odoo/odoo/odoo/modules/loading.py", line 194, in load_module_graph
registry.setup_models(cr)
File "/home/odoo/odoo/odoo/modules/registry.py", line 264, in setup_models
model._setup_base()
File "/home/odoo/odoo/odoo/models.py", line 2576, in _setup_base
self._add_field(name, field.new())
File "/home/odoo/odoo/odoo/fields.py", line 332, in new
return type(self)(**kwargs)
TypeError: __init__() missing 1 required positional argument: 'string'
2019-12-15 14:05:13,495 17591 CRITICAL testdb odoo.service.server: Failed to initialize database `testdb`.
Traceback (most recent call last):
File "/home/odoo/odoo/odoo/service/server.py", line 1116, in preload_registries
registry = Registry.new(dbname, update_module=update_module)
File "/home/odoo/odoo/odoo/modules/registry.py", line 86, in new
odoo.modules.load_modules(registry._db, force_demo, status, update_module)
File "/home/odoo/odoo/odoo/modules/loading.py", line 417, in load_modules
force, status, report, loaded_modules, update_module, models_to_check)
File "/home/odoo/odoo/odoo/modules/loading.py", line 313, in load_marked_modules
perform_checks=perform_checks, models_to_check=models_to_check
File "/home/odoo/odoo/odoo/modules/loading.py", line 194, in load_module_graph
registry.setup_models(cr)
File "/home/odoo/odoo/odoo/modules/registry.py", line 264, in setup_models
model._setup_base()
File "/home/odoo/odoo/odoo/models.py", line 2576, in _setup_base
self._add_field(name, field.new())
File "/home/odoo/odoo/odoo/fields.py", line 332, in new
return type(self)(**kwargs)
TypeError: __init__() missing 1 required positional argument: 'string'

Code:

https://github.com/notteccon/learn-odoo-custom-field-type

Issue:

https://github.com/notteccon/learn-odoo-custom-field-type/issues/1

Thanks


Avatar
Discard
Best Answer

Hello,

Try below code for Integer field.


class IntegerAge(Integer):   
    xml_size = 3   
    def __init__(self, string='Age', **kwargs):     
        super(Integer, self).__init__(string, **kwargs)
Thanks,
Avatar
Discard