我可以从注释中的 sales table 中获取 Sum(amount_sold) 的值吗
Can I get the value of Sum(amount_sold) from sales table inside the annotate
我正在尝试显示一个 html table,其中填充了数据库中的总和值。我有 sum_amount_purchased 和 sum_total_price_purchased 工作正常,但我正在努力 sum_amount_sold 来获得这个值,我需要查询销售额 table。这可能在注释中执行此操作吗?我想要该用户每种货币的所有销售额总和。任何建议将不胜感激。
Table以下
{% for transaction in transactions %}
<tr>
<td>{{transaction.currency}}</td>
<td>{{transaction.sum_amount_purchased}}</td>
<td>{{transaction.sum_total_price_purchased }}</td>
<td>{{transaction.sum_amount_sold}}</td>
</tr>
{% endfor %}
交易模式如下
class Transaction(models.Model):
currency = models.CharField(max_length=20)
amount = models.IntegerField()
total_price = models.DecimalField(max_digits=8, decimal_places=2)
date_purchased = models.DateTimeField()
note = models.TextField(default="")
owner = models.ForeignKey(User, on_delete=models.CASCADE)
amount_per_coin = models.DecimalField(max_digits=8, decimal_places=2, editable=False)
def save(self, *args, **kwargs):
self.amount_per_coin = self.total_price / self.amount
super(Transaction, self).save(*args, **kwargs)
def __str__(self):
return str(self.pk)+','+self.currency + ', '+str(self.amount)
def get_absolute_url(self):
return reverse('transaction-detail', kwargs={'pk': self.pk})
销售型号如下
class Sale(models.Model):
amount_sold = models.IntegerField()
total_price_sold = models.DecimalField(max_digits=8, decimal_places=2)
date_sold = models.DateTimeField(default=timezone.now)
note = models.TextField(default="")
transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name="sales")
amount_per_coin_sold = models.DecimalField(max_digits=8, decimal_places=2, editable=False)
def __str__(self):
return str(self.pk)+','+str(self.amount_sold) + ', '+self.note
def save(self, *args, **kwargs):
self.amount_per_coin_sold = self.total_price_sold / self.amount_sold
super(Sale, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('sale-detail', kwargs={'pk': self.pk})
查看下方
@login_required
def portfolio(request):
context = {
'transactions': Transaction.objects.filter(owner=request.user).values('currency').annotate(
sum_amount_purchased=Sum('amount'),
sum_total_price_purchased=Sum('total_price'),
sum_amount_sold=Sum('sales')
),
}
return render(request, 'webapp/portfolio.html', context, {'title': 'Portfolio'})
您需要使用双下划线语法遍历到您实际想要求和的 Sale 字段 - 大概是 amount_sold:
sum_amount_sold=Sum('sales__amoint_sold')
我正在尝试显示一个 html table,其中填充了数据库中的总和值。我有 sum_amount_purchased 和 sum_total_price_purchased 工作正常,但我正在努力 sum_amount_sold 来获得这个值,我需要查询销售额 table。这可能在注释中执行此操作吗?我想要该用户每种货币的所有销售额总和。任何建议将不胜感激。
Table以下
{% for transaction in transactions %}
<tr>
<td>{{transaction.currency}}</td>
<td>{{transaction.sum_amount_purchased}}</td>
<td>{{transaction.sum_total_price_purchased }}</td>
<td>{{transaction.sum_amount_sold}}</td>
</tr>
{% endfor %}
交易模式如下
class Transaction(models.Model):
currency = models.CharField(max_length=20)
amount = models.IntegerField()
total_price = models.DecimalField(max_digits=8, decimal_places=2)
date_purchased = models.DateTimeField()
note = models.TextField(default="")
owner = models.ForeignKey(User, on_delete=models.CASCADE)
amount_per_coin = models.DecimalField(max_digits=8, decimal_places=2, editable=False)
def save(self, *args, **kwargs):
self.amount_per_coin = self.total_price / self.amount
super(Transaction, self).save(*args, **kwargs)
def __str__(self):
return str(self.pk)+','+self.currency + ', '+str(self.amount)
def get_absolute_url(self):
return reverse('transaction-detail', kwargs={'pk': self.pk})
销售型号如下
class Sale(models.Model):
amount_sold = models.IntegerField()
total_price_sold = models.DecimalField(max_digits=8, decimal_places=2)
date_sold = models.DateTimeField(default=timezone.now)
note = models.TextField(default="")
transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE, related_name="sales")
amount_per_coin_sold = models.DecimalField(max_digits=8, decimal_places=2, editable=False)
def __str__(self):
return str(self.pk)+','+str(self.amount_sold) + ', '+self.note
def save(self, *args, **kwargs):
self.amount_per_coin_sold = self.total_price_sold / self.amount_sold
super(Sale, self).save(*args, **kwargs)
def get_absolute_url(self):
return reverse('sale-detail', kwargs={'pk': self.pk})
查看下方
@login_required
def portfolio(request):
context = {
'transactions': Transaction.objects.filter(owner=request.user).values('currency').annotate(
sum_amount_purchased=Sum('amount'),
sum_total_price_purchased=Sum('total_price'),
sum_amount_sold=Sum('sales')
),
}
return render(request, 'webapp/portfolio.html', context, {'title': 'Portfolio'})
您需要使用双下划线语法遍历到您实际想要求和的 Sale 字段 - 大概是 amount_sold:
sum_amount_sold=Sum('sales__amoint_sold')