Help

5

How to add a new value to a selection field (`state` in `sale.order`)?

Avatar
Nicolas Bustillos

I really need to add an additional 'state' value on my Sale Order object. Since version 7.0, the 'sale_stock' module does exactly that already. When you try to do the same thing from your own module, your key,value just gets ignored. Is there any other alternative to achieve this?
As I found out, this seems to be an old time issue from two years ago as explained in this thread. A suggested workaround there was to do something like this:

_inherit = 'sale.order'
def __init__(self, pool, cr):
    super(sale_order, self)._columns['state'].selection.append(('keyx', 'valuex'))

I found this approach logical, but it resulted in the following error:

`File "/home/nicolas/Eclipse/OpenERP/7.0/src/openerp/osv/orm.py", line 2958, in _auto_init
    self._field_create(cr, context=context)
File "/home/nicolas/Eclipse/OpenERP/7.0/src/openerp/osv/orm.py", line 764, in _field_create
    ir_model_fields_obj = self.pool.get('ir.model.fields')
AttributeError: 'sale.order' object has no attribute 'pool'`

Should this bug be reported at launchpad or is it an unintended use? What other possible solutions can you suggest? Thanks in advance.

1 Comment
Avatar
Discard

@Nico Bustillos, Is this issue solved by changing the whole state when inherited by other modules or done using other method?

4 Answers
17
Avatar
John
Best Answer

Hi, In V8 you have selection_add attribute, it can be used like

state = fields.Selection(selection_add=[('early_payment', 'Early payment: Discount early payment')])
1 Comment
Avatar
Discard
Avatar
Gopakumar N G
-

The newly added state always comes last in the header even after specifying statusbar_visible field in proper order. Is there anyway to correct it?

For example, the existing states are Draft->Post and I have added 'Validated' using selection_add but is coming in the form view as Draft->Post->Validated and what I was expecting is Draft->Validated->Post(statusbar_visible="draft,validate,post").

7
Best Answer

Redefine the state field and make your module depending on sale_stock instead of sale (in __openerp__.py). That way, you insure your object will be the last to update the state field properties.

In OpenERP inheritance, when several classes try to update the same field, the last one will win.

You cannot use the __init__() because the inheritance tree is built after instantiation by the ORM. When __init__() is called, your object would not have sale_order class as an ancestor or pool attribute.

2 Comments
Avatar
Discard
Avatar
Nicolas Bustillos
-

Thank you. Indeed this simple change solves my particular problem. I guess in some other cases where the 3rd party addon is unknown this inherit issue could still be a problem. Fortunately, in my case 'sale_stock' is among the certified addons and I have no problem including it as a dependency. Thanks again.

Avatar
Pouya Malekinejad
-

Hi, in new api the correct way is to use selection_add attribute as in the documentation: class openerp.fields.Selection(selection=None, string=None, **kwargs) Bases: openerp.fields.Field Parameters: selection – specifies the possible values for this field. It is given as either a list of pairs (value, string), or a model method, or a method name. selection_add – provides an extension of the selection in the case of an overridden field. It is a list of pairs (value, string). The attribute selection is mandatory except in the case of related fields or field extensions.

4
Best Answer

Hi,

to add a field type selection in the class, do like this :

class sale_order(osv.osv):
   _inherit = 'sale.order'
   _columns = {
       'state': fields.selection((('keyx', 'valuex'), ('keyz', 'valuez')), 'State'),
   }
   _defaults = {
       'state': 'keyx',  
   }
sale_order()

Hope this will help and work.

Bye

2 Comments
Avatar
Discard
Avatar
Nicolas Bustillos
-

This would be the standard way to add a value to any given 'selection' field and it usually works. The key problem here however is that there is a 3rd addon that does exactly that and is being priorized upon my own module, which ultimately gets ignored. But as you can see from the correct answer given, it is just a matter of making the dependencies lineal among the three addons (instead of both addons inheriting from a single one). Also note that I don't want to change my new value as the default one, as I still need 'draft' to be the initial state. Thanks a lot anyway.

Hi, then you could use states parameter too, this will follow the workflow and depend not of the dependencies, it will overwrite all properties, see openerp developer documentation : http://doc.openerp.com/v6.1/developer/03_modules_2.html .thanks for the comment. Bye

3
Avatar
NUMERIGRAPHE
Best Answer

Hi, Please note that super(...)._columns['state'] will only work if no other module has a _inherit to the same class. Otherwise, super(...)._columns may contain the columns added/modified by the other module.

To achieve this cleanly, the main class must be patched to make the state selection a class attribute. This has not been done for Sale Orders yet but it looks like something you could reasonably ask for. Check out Purchase Orders for example:

class purchase_order(osv.osv):
    ...
    STATE_SELECTION = [
        ('draft', 'Draft PO'),
        ('sent', 'RFQ Sent'),
        ('confirmed', 'Waiting Approval'),
        ('approved', 'Purchase Order'),
        ('except_picking', 'Shipping Exception'),
        ('except_invoice', 'Invoice Exception'),
        ('done', 'Done'),
        ('cancel', 'Cancelled')
    ]
    ...
    _columns = {
        ...
        'state': fields.selection(STATE_SELECTION, 'Status', ...),
        ...
    }

Then you can add states like this:

class PurchaseOrder (osv.osv):
    _inherit = 'purchase.order'    
    def __init__(self, pool, cr):
        """Add a new state value"""
        super(PurchaseOrder, self).STATE_SELECTION.append(('foo', 'Foo'))
        return super(PurchaseOrder, self).__init__(pool, cr)
2 Comments
Avatar
Discard
Avatar
Mohammad Alhashash
-

This is completely wrong. When __init__() method is called, the inherited class would not be in the MRO chain and is not reachable through super(). That is why the OP got the missing pool attribute error; the model was not initialized yet and has no pool attribute. This is not standard python inheritance. You can use init() method which is called by the ORM after initialization, but this will not solve the problem too.

Avatar
Mohammad Alhashash
-

Having the list as a class attribute is irrelevant because model._columns['state'].selection is a reference to that list whether it is inline, class or module variable and using it is the most reliable way to reach the list. The problem of the OP was that sale_stock module is replacing the list with a new one that overrides his list. Check my answer for the correct solution.