我想在ClassBasedView的get_context_data方法中将总值作为收益变量返回。
我还想返回另一个操作:total
-shipping_cost
作为上下文中的revenue_no_shipping_cost
变量。
我试过了:
from django.db.models import F, Sum
class OrdersListView(PermissionRequiredMixin,ListView):
model = Order
permission_required = 'is_staff'
template_name = "order/revenue.html"
paginate_by = 10
def get_queryset(self):
filter_month = self.request.GET.get('filtromes', '0')
if filter_month == "0":
return Order.objects.filter(status = 'recibido_pagado')
else:
return (Order.objects
.filter(created__month=filter_month, status = 'recibido_pagado'))
def get_context_data(self, **kwargs):
context = super(OrdersListView, self).get_context_data(**kwargs)
qs = kwargs.pop('object_list', self.object_list)
order = self.request.GET.get('orderby', 'created')
context = {}
revenue = qs.aggregate(revenue=Sum('total'))
revenue_no_shipping = qs.annotate(revenue_no_shipping=F('total') + F('shipping_cost'))
context['revenue'] = revenue
context['revenue_no_shipping'] = revenue_no_shipping
context['filtromes'] = self.request.GET.get('filtromes', '0')
context['orderby'] = self.request.GET.get('orderby', 'created')
context['category'] = "catalogo"
return context
但是在模板中,我得到:
收入:
{'revenue': Decimal('42')} #is 42.00
对于Revenue_no_shipping:
<QuerySet [<Order: 58>]> #Should be 25.00
models.py:
class Order(models.Model):
ORDER_STATUS = (
('recibido_pagado', 'Recibido y pagado'),
('recibido_no_pagado', 'Recibido pero no pagado'),
('en_proceso', 'En proceso'),
('en_camino', 'En camino'),
('entregado', 'Entregado'),
('cancelado', 'Cancelado por no pagar' )
)
token = models.CharField(max_length=100, blank=True, null=True)
first_name = models.CharField(max_length=50, blank=True, null=True)
last_name = models.CharField(max_length=50, blank=True, null=True)
phone_number = models.CharField(max_length=30, blank=True)
total = models.DecimalField(max_digits=10, decimal_places=2)
stickers_price = models.DecimalField(max_digits=10, decimal_places=2)
discount = models.DecimalField(max_digits=10, decimal_places=2, default=Decimal('0.00'))
shipping_cost = models.DecimalField(max_digits=10, decimal_places=2)
email = models.EmailField(max_length=250, blank = True, verbose_name= 'Correo electrónico')
last_four = models.CharField(max_length=100, blank=True, null=True)
created = models.DateTimeField(auto_now_add=True)
shipping_address = models.CharField(max_length=100, blank=True, null=True)
shipping_address1 = models.CharField(max_length=100, blank=True, null=True)
reference = models.CharField(max_length=100, blank=True, null=True)
shipping_department = models.CharField(max_length=100, blank=True, null=True)
shipping_province = models.CharField(max_length=100, blank=True, null=True)
shipping_district = models.CharField(max_length=100, blank=True, null=True)
reason = models.CharField(max_length=400, blank=True, null=True, default='')
status = models.CharField(max_length=20, choices=ORDER_STATUS, default='recibido_pagado')
comments = models.CharField(max_length=400, blank=True, null=True, default='')
cupon = models.ForeignKey('marketing.Cupons', blank=True, null=True, default=None, on_delete=models.SET_NULL)
class Meta:
db_table = 'Order'
ordering = ['-created']
def __str__(self):
return str(self.id)
def igv(self):
igv = int(self.total) * 18/100
return igv
def shipping_date(self):
shipping_date = self.created + datetime.timedelta(days=10)
return shipping_date
def deposit_payment_date(self):
deposit_payment_date = self.created + datetime.timedelta(days=2)
return deposit_payment_date
更新1:
模板中的HTML
<h1>Ordenes recibidas y pagadas:</h1>
<p> {{ revenue }}</p>
<h1>Ordenes recibidas y pgadas (no shipping)</h1>
<p> {{ revenue_no_shipping }}</p>
更新了get_context_data方法:
def get_context_data(self, **kwargs):
context = super(OrdersListView, self).get_context_data(**kwargs)
qs = kwargs.pop('object_list', self.object_list)
order = self.request.GET.get('orderby', 'created')
context = {}
revenue = qs.aggregate(revenue=Sum('total'))
revenue_no_shipping = qs.aggregate(revenue_no_shipping=Sum(F('total') + F('shipping_cost')))
context['revenue'] = revenue
context['revenue_no_shipping'] = revenue_no_shipping
context['filtromes'] = self.request.GET.get('filtromes', '0')
context['orderby'] = self.request.GET.get('orderby', 'created')
context['category'] = "catalogo"
return context
最后答案:
根据
jmunsch
的答案,我得到了正确的答案,只需添加: revenue = float(revenue['revenue']) #necessary to properly render in template / not as Decimal
revenue_no_shipping = float(revenue_no_shipping['revenue_no_shipping']) #necessary to properly render in template / not as Decimal
在模板中正确呈现。
def get_context_data(self, **kwargs):
context = super(OrdersListView, self).get_context_data(**kwargs)
qs = kwargs.pop('object_list', self.object_list)
order = self.request.GET.get('orderby', 'created')
context = {}
revenue = qs.aggregate(revenue=Sum('total'))
revenue_no_shipping = qs.aggregate(revenue_no_shipping=Sum(F('total') - F('shipping_cost')))
revenue = float(revenue['revenue']) #necessary to properly render in template / not as Decimal
revenue_no_shipping = float(revenue_no_shipping['revenue_no_shipping']) #necessary to properly render in template / not as Decimal
context['revenue'] = revenue
context['revenue_no_shipping'] = revenue_no_shipping
context['filtromes'] = self.request.GET.get('filtromes', '0')
context['orderby'] = self.request.GET.get('orderby', 'created')
context['category'] = "catalogo"
return context
最佳答案
对于使用django求和两列的方法,这样的事情可能起作用:
qs.aggregate(revenue_no_shipping=Sum(F('total') - F('shipping_cost')))