Skip to Content
Menu
This question has been flagged
5 Replies
4227 Views

I am using odoo 9. I would like to find a way to show 2 columns on a dropdown selection. For instance, when clicking on the lot_name field in the stock.pack.operation.lot tree view, I would like to show the lot_name and the available_qty of a product in the dropdown list, similar to the grid-like-multi-columm-combo box in the following link (https://www.obout.com/combobox/aspnet_columns_grid.aspx). Is it possible to have such grid-like-multi-columm-combo box in odoo? If yes, where can I find some examples and tutorial. Thanks.

Simon Lee

Avatar
Discard
Author Best Answer

Hengky Zhang, thank you for your answer. I finally have time to try this out but I ran into this problem.

Here's my code:

.py file:
class my_stock_production_lot(models.Model):
    _name = 'stock.production.lot'   
    _inherit = 'stock.production.lot'    
    product_available_qty=fields.Float(string='Quantity Available', related='product_id.qty_available')           

    @api.multi   
    def name_get(self):
        res = []       
        for lot in self:
            if lot.name:                  
                display_value = lot.name               
                if lot.product_available_qty > 0:
                    display_value += ' ['                   
                    display_value += '%.0f Units' % lot.product_available_qty                   
                    display_value += ']'               
                res.append((lot.id, display_value))        
        return res

.xml file:
<record id="view_pack_operation_lot_form_inherit" model="ir.ui.view">           
<field name="inherit_id" ref="stock.view_pack_operation_lot_form"/>           
<field name="model">stock.pack.operation</field>           
<field name="arch" type="xml">               
<xpath expr="//field[@name='pack_lot_ids']/tree/field[@name='lot_id']" position="replace">                   
<field name="lot_id" invisible="context.get('only_create', False)" 
        domain="[('product_id','=', parent.product_id)]"                                      
        context="{'product_id': parent.product_id, 'show_qty': True}"/>               
</xpath>           
</field>                   
</record>

My question:
1) Was I right on how to include 'show_qty' in the context of the .xml file?

2) I cannot reference  context in the name_get method. name_get cannot accepts a context parameter. When I checked the self._context variable in the debugger, I did not see {'show_qty': False}. I cannot  do "if self._context.get('show_qty'):..." either since it is a frozen dict. So how can I determine in name_get whether I should display the default name or my customize name[qty]???

Can you please explain possibly with example? Thanks.

Avatar
Discard
Best Answer

Yes you can, but not same 100%

you override method name search in that many2one object

so you can override the return value

for default odoo just return the name of the relation table, and what you need to need to substring that name with the quantity example odoo will return --> LOT001(10QTY) instead of LOT001

for selection field you should use method to get the content of that selectional field and use your logical code for the return

Avatar
Discard
Author

Thank you for the idea. I have a question. If I override name_search and add qty to the name, all other functions that call name_search will see the name as LOT001(10 PCS), right? I don't want such global effect. Just want to show the name+qty in some specific view. How can I do that? Thanks.

sorry the method should be name_get not name search ^_^

lucky you understand what i mean

yes that method is global, let me check it first is it possible for specific field

yes it is possible for specific field friend,

you need to add the context in the field

<field name="product_id" context="{attribute_only':True}"/>

then in the name_get method put the IF statement if the context is True then bla bla bla