def sum_quantity(good):
return sum(map(lambda s: s["quantity"], store[goods[good]]))
def sum_cost(good):
return sum(map(lambda s: s["quantity"] * s["price"], store[goods[good]]))
def format_good(good):
return f'{good} -{sum_quantity(good)} шт, стоимость {sum_cost(good)}руб'
print('\n'.join(map(format_good, goods)))
Простите, а как его использовать в модели, т.е. как заменить мне вот этот код?
class ColorField(models.CharField):
""" Поле для хранения HTML-кода цвета."""
def __init__(self, *args, **kwargs):
kwargs.setdefault('max_length', 7)
super().__init__(*args, **kwargs)
self.validators.append(validators.RegexValidator(r'#[a-f\d]{6}'))
class MyAdmin(ModelAdmin):
formfield_overrides = {
ColorField: {'widget': forms.TextInput(attrs={'type': 'color'})}
}
SELECT SUM(FIRST_VALUE(o.total) OVER w)
FROM order o
LEFT JOIN
order_history oh ON o.order_id = oh.order_id
WHERE
o.payment_code = 'cod'
AND o.order_status_id NOT IN (7,21)
AND oh.order_status_id = 16
AND MONTH(oh.date_added) = MONTH(NOW()) AND YEAR(oh.date_added) = YEAR(NOW())
WINDOW w AS (PARTITION BY o.order_id ORDER BY oh.date_added
ROWS UNBOUNDED PRECEDING);
SELECT SUM(t.total) FROM (
SELECT MAX(o.total)
FROM
order o
LEFT JOIN
order_history oh ON o.order_id = oh.order_id
WHERE
o.payment_code = 'cod'
AND o.order_status_id NOT IN (7,21)
AND oh.order_status_id = 16
AND MONTH(oh.date_added) = MONTH(NOW()) AND YEAR(oh.date_added) = YEAR(NOW())
GROUP BY o.id
) t