问题描述
我正在尝试使用order_total属性,将django管理员变更列表中某个孩子的订单项相加。 order_total属性在我的列表视图中正确显示,但在图表功能中不起作用。我在做什么错了?
models.py
class Order(models.Model):
class Meta:
ordering = ['date_needed']
date_ordered = models.DateField(blank=True,null=True,default=datetime.Now)
date_needed = models.DateField(blank=True,default=datetime.Now)
date_shipped = models.DateField(blank=True,null=True)
customer = models.ForeignKey(Customer,on_delete=models.CASCADE,related_name='orders')
customer_order = models.CharField(max_length=100,blank=True,null=True)
status = models.ForeignKey(Order_Status,on_delete=models.CASCADE)
invoice_no = models.CharField(max_length=45,null=True)
tracking_no = models.CharField(max_length=45,null=True)
shipping_cost =models.DecimalField(max_digits=10,decimal_places=2,null=True)
carrier = models.CharField(max_length=50,null=True)
service = models.CharField(max_length=100,null=True)
@property
def order_total(self):
l = []
for item in self.order_items.all():
p = self.customer.pricings.filter(offering=item.offering).first().price if self.customer.pricings.filter(offering=item.offering).first() is not None else item.offering.wholesale_price,l.append(p[0] * item.lbs)
return sum(l)
**admin.py**
class OrderAdmin(admin.ModelAdmin):
inlines = [OrderItemInline]
list_display = ['customer','order_number','order_total','date_ordered','status']
ordering = ['-date_ordered']
def changelist_view(self,request,extra_context=None):
response = super(OrderAdmin,self).changelist_view(request,extra_context)
filtered_query_set = response.context_data["cl"].queryset
chart_data = (
filtered_query_set.annotate(date=TruncDay("date_ordered"),month=ExtractMonth('date_ordered'))
.values("date",'month')
.annotate(y=sum("order_total"))
.order_by("-date_ordered")
)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)