python - django 按日归档统计订单求解
PHP中文网
PHP中文网 2017-04-18 10:33:07
0
1
556

models.py:订单数据模型

class Order(models.Model):
    date = models.DateTimeField(default=timezone.now)
    paid_card = models.PositiveIntegerField(blank=True, null=True)
    paid_cash = models.PositiveIntegerField(blank=True, null=True)
    paid_gift = models.PositiveIntegerField(blank=True, null=True)
    
    statur = models.CharField()

    @property
    # 实收金额(paid_in) = paid_card + paid_cash + paid_gift
    def paid_in(self):
        return sum(filter(None, [self.paid_card, self.paid_cash, self.paid_gift]))

views.py: 按日归档统计每天的订单数、订单总额

def wx_archive(request):
    orders = Order.objects.filter(status='closed')\
        .annotate(day=TruncDay('start_time')).values('day')\
        .annotate(count=Count('id'), total=Sum('paid_in')).values('day', 'count', 'total')\
        .order_by('day')
    return render(request, 'orders/archive.html', {'orders': orders})

运行后报错,提示

FieldError at /orders/archive/
Cannot resolve keyword 'paid_in' into field. Choices are: count, day, id, paid_card, paid_cash, paid_gift, status

请问,我该如何统计每日的总 paid_in

PHP中文网
PHP中文网

认证0级讲师

reply all(1)
刘奇

Your paid_in is not a database field and obviously cannot be used in SQL statements. Two methods:

  1. Take out the orders instances that match the time period and filter them by time. Then Sum(all order.pard_in)

  2. Database level Sum paid_card, paid_cash, paid_gift of all orders that match the time period

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!