如何使用odoo查询进行过滤,搜索,分组

时间:2019-07-02 05:19:30

标签: python odoo-10

在库存移动中,我想按某个特定日期进行过滤,并按product_id,location_dest_id和product_uom_quantity进行分组。

@api.multi
    def compute_report(self):

       record_move= self.env['stock.move'].search([('date','>=',self.start_date)]).
        .read_group([('date','>=',self.start_date)],[sum('product_uom_quantity')],['product_id','location_dest_id'])
        return {'record_move': record_move}

1 个答案:

答案 0 :(得分:2)

domain = [('date', '=', specific_date),
          ('product_id', '=', id_of_the_product),
          ('location_dest_id', '=', id_of_location)]
record = self.env['stock.move'].search(domain)
sum=0
for rec in record:
   sum = sum + rec.product_uom_quantity
return sum

I want to groupby all product_id and location_dest_id since i have plenty articles

然后将所有product_id和location_dest_id读入列表

p = self.env['model_of_product'].search([])


_product=[]
# collect all product id
for product in p:
    _product.append(product.id)

box = []
for _p in _product:
    domain = [  ('date', '=', specific_date),
                ('product_id', '=', _p)]
    record = self.env['stock.move'].search(domain)
    sum_of_stock = 0
    sum_of_client = 0
    # box will contain sets of [product_id, sum_of_stock, sum_of_client ]
    for rec in record.search([('name', '=', 'Clients')]):
        sum_of_client = sum_of_client + rec.product_uom_quantity
    for rec in record.search([('name', '=', 'Stock')]):
        sum_of_stock = sum_of_stock + rec.product_uom_quantity

    box.append([_p, sum_of_stock, sum_of_client])
return box