如何在不同选项卡中按字段搜索产品

2024-04-27 03:38:24 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在产品中使用名称搜索,通过搜索stock.production.lot中的批次名称来获得产品

relation stock_quant_ids,product_id,lot_id

class ProductProduct(models.Model):
    _inherit = 'product.product'

    @api.model
    def name_search(self, name='', args=None, operator='ilike', limit=100):

        args = args or []
        print('arg ',args)
        recs = self.search([('???', operator, name)]  args, limit=limit) #stock_quant_ids.lot_id.name
        print('recs ', recs)
        if not recs.ids:
            return super(ProductProduct, self).name_search(name=name, args=args,
                                                       operator=operator,
                                                       limit=limit)
        return recs.name_get()

Tags: nameself名称ididssearch产品stock
1条回答
网友
1楼 · 发布于 2024-04-27 03:38:24

试试这个:

@api.model
def name_search(self, name='', args=None, operator='ilike', limit=100):
    """ search for product using the Lot number """
    args = args or []
    recs = None
    # only perform search by when the name is passed
    if name:
        # don't use limit here
        recs = self.env['stock.production.lot'].search([('name', operator, name)])

    if recs:
        # handle extra filter that was passed by the domain attribute in the XML
        args = expression.AND([args, [('id', 'in', recs.mapped('product_id').ids)]])
        return self.search(args, limit=limit).name_get()

    # no Lot was founded may be the user meant to search by product name
    return super(ProductProduct, self).name_search(name=name,
                                            args=args,
                                            operator=operator,
                                            limit=limit)

相关问题 更多 >