您好,我有一个代码给我dict这个dict是:
{2516: {'order_date': datetime.datetime(2021, 7, 26, 16, 10, 10, 461000), 'product_id': 2516,
'product_name': 'TRENTO GRIS 30*60 NEW TILE SPAIN 514', 'quantity': -1.0, 'product_cost': 4.0,
'price_unit': 5.5, 'remaining_qty': -23.0, 'seller_name': 'السيد ابو فهد', 'jour': 'Cash,'},
2412: {'order_date': datetime.datetime(2021, 7, 26, 13, 59, 33, 351000), 'product_id': 2412,
'product_name': 'ORION HEXA SILVER 19.8*22. SPAIN 355', 'quantity': -4.0, 'product_cost': 0.58,
'price_unit': 1.25, 'remaining_qty': 129.0, 'seller_name': 'السيد برغش المطيري المحترم ', 'jour': 'Cash,'},
2897: {'order_date': datetime.datetime(2021, 7, 23, 20, 4, 2, 939000), 'product_id': 2897, 'product_name': 'BLANCO BR RECT LA 30*60 SPAIN 456',
'quantity': -6.0, 'product_cost': 3.5, 'price_unit': 4.0, 'remaining_qty': 6.0, 'seller_name': 'السيد احمد منصور العازمي',
'jour': 'Cash,'},
2828: {'order_date': datetime.datetime(2021, 7, 23, 20, 8, 38, 380000),
'product_id': 2828, 'product_name': 'كاونتر مدور رمادي فاتح 1277 (36*12 CM', 'quantity': -1.0, 'product_cost': 0.0,
'price_unit': 27.5, 'remaining_qty': -1.0, 'seller_name': 'السيد علي المطيري المحترم', 'jour': 'Cash,'},
3041: {'order_date': datetime.datetime(2021, 7, 23, 20, 7, 11, 189000), 'product_id': 3041, 'product_name': 'PRIS TINENZA 30*60 SPAIN',
'quantity': -2.0, 'product_cost': 5.0, 'price_unit': 6.5, 'remaining_qty': -13.0, 'seller_name': 'السيد محمد حسن الكندري المحترم', 'jour': 'Cash,'}}
我尝试了列表压缩,如:
if products_sold.get(line.product_id.id):
products_sold[line.product_id.id] = sorted([{
'order_date': order.date_order,
'product_id': products_sold[line.product_id.id].get('product_id'),
'product_name': products_sold[line.product_id.id].get('product_name'),
'quantity': products_sold[line.product_id.id].get('quantity', 0) + line.qty,
'product_cost': products_sold[line.product_id.id].get('product_cost'),
'price_unit': products_sold[line.product_id.id].get('price_unit'),
'remaining_qty': line.product_id.qty_available,
'seller_name': seller_name,
'jour': jour
} for (product, seller_name, order, line, jour), qty in products_sold.get(line.product_id.id)],key=lambda l: l['order_date']
但是每次我尝试这个,我都会得到一个错误"ValueError: too many values to unpack (expected 2)"
我甚至试过:
dict = {k: v for k, v in sorted(products_sold[line.product_id.id].items(), key= lambda v : v[0])}
并将其传递给我,但我仍然得到了错误
如何对上面的字典进行排序我不知道会得到什么帮助,下面是一个方法,我希望通过"order_date"
对dict进行排序:
def get_sale_details_product_wise(self, date_start=False, date_stop=False, configs=False):
if not configs:
configs = self.env['pos.config'].search([])
orders = self.env['pos.order'].search([
('date_order', '>=', date_start),
('date_order', '<=', date_stop),
('state', 'in', ['paid', 'invoiced', 'done']),
('config_id', 'in', configs.ids)])
products_sold = {}
for order in orders:
jour = ''
jour_list = []
for statement in order.statement_ids:
if statement.journal_id.name not in jour_list:
jour_list.append(statement.journal_id.name)
jour += statement.journal_id.name + ','
for line in order.lines:
seller_name = self.env["res.partner"].search(
[('id', '=', line.order_id.partner_id.id)]).name
if products_sold.get(line.product_id.id):
# print('1111111', line.product.id)
products_sold[line.product_id.id] = sorted([{
'order_date': order.date_order,
'product_id': products_sold[line.product_id.id].get('product_id'),
'product_name': products_sold[line.product_id.id].get('product_name'),
'quantity': products_sold[line.product_id.id].get('quantity', 0) + line.qty,
'product_cost': products_sold[line.product_id.id].get('product_cost'),
'price_unit': products_sold[line.product_id.id].get('price_unit'),
'remaining_qty': line.product_id.qty_available,
'seller_name': seller_name,
'jour': jour
} for (product, seller_name, order, line, jour), qty in products_sold.get(line.product_id.id)],key=lambda l: l['order_date'])
else:
products_sold[line.product_id.id] = {
'order_date': order.date_order,
'product_id': line.product_id.id,
'product_name': line.product_id.name,
'quantity': line.qty,
'product_cost': line.product_id.standard_price,
'price_unit': line.price_unit,
'remaining_qty': line.product_id.qty_available,
'seller_name': seller_name,
'jour': jour
}
return {'products': products_sold}
这种说法是错误的:
您正在迭代
products_sold.get(line.product_id.id)
的值,并期望每个项包含5个变量的序列。products_sold.get(line.product_id.id)
的值是一个dict:输出:
这意味着迭代
products_sold.get(line.product_id.id)
等同于迭代键products_sold.get(line.product_id.id).keys()
,它们只是字符串:输出:
因此,您尝试将每个字符串分配给这5个变量:
显然,它们不适合,这就是您遇到错误
ValueError: too many values to unpack (expected 2)
的原因解决方案
下面是如何根据
order_date
属性对所有products_sold
进行排序输出:
样本运行:
这假设您使用的是Python 3.7或更高版本,如documented,因为只有从该版本订购字典。否则,使用一个列表(或collections.orderedict),因此将上面的更改为
list(sorted(...
相关问题 更多 >
编程相关推荐