Search code examples
pythonxmlpython-3.xodooodoo-12

How to filter column by other column in odoo 12?


I try to filter an "item" column that has a many2one type based on the names that already exist in another "itemsnames" column that has a char type, I can not write the domain in the xml file, here is my xml file:

<record id="sale_order_line_form_inherited" model="ir.ui.view">
            <field name="name">sale_order.line.form.inherited</field>
            <field name="model">sale.order</field>
            <field name="inherit_id" ref="sale.view_order_form"/>
            <field name="arch" type="xml">
                <xpath expr="//tree//field[@name='name']" position="after">
                    <field name="categorie"/>
                    <!--<field name="item" domain="[('item.name','=', 'name.name')]"/>-->
                    <!--<field name="item" domain="[('item.name','in',items)]"/>-->
                    <field name="item" domain="[('item.name','in',itemsnames)]"/>
                    <field name="itemsids"/>
                    <field name="itemsnames"/>
                </xpath>
            </field>
        </record>

Here is python file:

class class2(models.Model):
    _name = 'module.item'
    name = fields.Char(string='item')


class class3(models.Model):
    _name = 'module.categorieitem'
    name = fields.Many2one('module.categorie', string='Categorie')
    item = fields.Many2one('module.item', string='Item')


class class4(models.Model):
    _inherit = 'sale.order.line'
    categorie = fields.Many2one('module.categorie')

    # list1=[1,2]
    # item = fields.Many2one('module.item', domain=[('id', '=', list1)])
    item = fields.Many2one('module.item')
    itemsids = fields.Char()
    itemsnames = fields.Char()

    @api.onchange('categorie')
    def _onchange_categorie(self):
        print('»»» call onchange_categorie')
        res = {}
        items_ids = []
        items_names = []
        records = self.env['module.categorieitem'].search([('name','=',self.categorie .id)])
        for record in records:
            print(record.item.name)
            items_ids.append(record.item.id)
            items_names.append(record.item.name)
        self.itemsids = items_ids
        self.itemsnames = items_names
        print('itemsnames: ',self.itemsnames)
        res['domain'] = {'item':[('id','in',items_ids)]}
        return res

Here is screenshot for view

view


Solution

  • class SalesController(http.Controller):

    @http.route('/rounds/details', type='http', auth="public", website=True)
    def pos_details_json(self):
        rounds = http.request.env['sale.order.line'].search([])
        my_list = []
        for data in rounds:
            my_list.append({'Line Name': data.item.name})
    
        return json.dumps(my_list)