Skip to Content
Menu
This question has been flagged
1 Reply
2256 Views
I was using a function to load list in selection field. It was working in Odoo16. When I checked same code in Odoo17 It is not working. The code added below.



from odoo.exceptions import UserError
from odoo import models, api, _, fields
import logging

_logger = logging.getLogger(__name__)
class TestOrderShop(models.TransientModel):
    _name = 'test.shop.wizard'
    _description = 'Test Shop'
   
    @api.model
    def default_get(self, fields):
   
        res = super(TestOrderShop, self).default_get(fields)     

        selected_ids = self.env['sale.order']._context.get('active_ids', [])
        _logger.info('\n\n\n\n  selected_ids  =  %s \n\n\n\n' % (selected_ids) )     
       
        return res   
   
   
    def _get_radio_button(self):
        selection=[
            ('cancel', "Cancel"),
            ('pending', "Pending"),
            ('ready', "Ready"),
        ]
        selected_ids = self.env['sale.order']._context.get('active_ids', [])
        _logger.info('\n\n\n\n  selected_ids  =  %s \n\n\n\n' % (selected_ids) )   
        #Conext value worked in Odoo16 
        #Not getting Context value here in Odoo17
       
       
        order_id = self.env['sale.order'].sudo().search([('id', 'in', selected_ids)])
       
        _logger.info('\n\n\n\n  _get_selection  =  %s \n\n\n\n' % (order_id.id) )   
             
        if order_id:     
            if not order_id.custom_id:
                selection=[('new_order', "New Order"),]
                return selection

        return selection


    test_block_id = fields.Many2one('test.block')
    shop_selection = fields.Selection(_get_radio_button, string="Shop Action")
   



    def shop_selection_action(self):
        shop_id = self.test_block_id
        if self.shop_selection == "pending":
            self.env['sale.order'].shop_pending_order(shop_id)
        if self.shop_selection == "ready":
            self.env['sale.order'].shop_order_ready(shop_id)                           
        if self.shop_selection == "cancel":           
            self.env['sale.order'].shop_order_cancel(shop_id)

return


Avatar
Discard
Best Answer

In context you get both active_model and active_ids. You can use self._context.get('') or self.env.context.get('') to get appropriate values.

For example,

self.env['active_model'].search('id', 'in', self._context.get('active_ids'))

Avatar
Discard
Author

shop_selection = fields.Selection(_get_radio_button, string="Shop Action")

I am using _get_radio_button() function to load the radio button values as per conditions.
Context gives correct values other functions.

But when using same context code in the _get_radio_button() to get the currently active id of the sale_order,
it just return empty in Odoo17 for me.

You have to pass context for sale order id in the action from where you are open a wizard and then you can use this context value in your relative method of wizard.

Author

I tried passing sale order id in context of the action that calls the wizard. But the data not getting inside the _get_radio_button() function.
In other functions like default_get(), I can access the context data.

Please paste the opening wizard method code here.. If I'm not wrong you're opening that wizard sale order, Right?

Author

I am opening the wizard from the sale order actions. Above code is the wizard code.

when printing action i get below result in log.
context data is getting in the default_get().
Not in the _get_radio_button() function which used to return radio button list as per condition.

{'id': 585,
'name': 'Test Shop',
'type': 'ir.actions.act_window',
'xml_id': 'my_test_shop.action_test_shop_actions_wizard',
'help': False,
'binding_model_id': False,
'binding_type': 'action',
'binding_view_types': 'list,form',
'display_name': 'Test Shop',
'create_uid': (1, 'OdooBot'),
'create_date': datetime.datetime(2024, 1, 9, 7, 32, 32, 966062),
'write_uid': (1, 'OdooBot'),
'write_date': datetime.datetime(2024, 1, 15, 6, 13, 30, 916019),
'view_id': (1549, 'test.shop.wizard.form.view'),
'domain': False,
'context': "{'test_block_id': 1, 'order_id': 1167}",
'res_id': 0,
'res_model': 'test.shop.wizard',
'target': 'new',
'view_mode': 'form',
'mobile_view_mode': 'kanban',
'usage': False,
'view_ids': [],
'views': [(1549, 'form')],
'limit': 80, 'groups_id': [],
'search_view_id': False,
'filter': False}

sale_ids = fields.many2many()

@api.model
def default_get(self, fields):

res = super(TestOrderShop, self).default_get(fields)

selected_ids = self.env['sale.order']._context.get('active_ids', [])
res['sale_ids'] = [(4, selected_ids)]
_logger.info('\n\n\n\n selected_ids = %s \n\n\n\n' % (selected_ids) )

return res

You can use sale_ids in radio button method.

Author

I tried similar and this method. But seems _get_radio_button() function is calling before the default_get() function.
So in log the variable value showed unknown in _get_radio_button() function.

selected_ids = _unknown()

Related Posts Replies Views Activity
4
May 25
1028
2
May 25
4066
1
Mar 25
501
4
Mar 25
3175
3
Feb 25
3809