[英]how to filter year from datefield in django
基本上我想使用'chartjs'來顯示“聚集條形圖”,每年都有以下事情
總金額 收到金額 剩余金額
我想從數據庫中獲取數據並將其顯示在圖表上,我已將每個日期的所有上述字段 3 字段的數據保存在一個表中,並且我還想從 Datefield 中獲取年份。
主要是如何通過從數據庫中獲取每年的數據來顯示集群條形圖。
class Allinvoice(models.Model):
company_choice = (
('VT_India', 'VT_India'),
('VT_USA', 'VT_USA'),
)
company = models.CharField(
max_length=30, blank=True, null=True, choices=company_choice)
customer = models.ForeignKey(Customer, on_delete=models.CASCADE)
project = models.ForeignKey(Allproject, on_delete=models.CASCADE)
invoice_title = models.CharField(max_length=15)
invoice_id = models.IntegerField(primary_key=True)
currency = models.ForeignKey(Currency, on_delete=models.CASCADE)
invoice_amount = models.IntegerField()
invoice_date = models.DateField(
blank=True, null=True)
invoice_duedate = models.DateField(
blank=True, null=True)
invoice_description = models.TextField()
def __str__(self):
return str(self.invoice_id)
class Paymentdete(models.Model):
payment_id = models.IntegerField(primary_key=True)
invoice = models.ForeignKey(
Allinvoice, on_delete=models.CASCADE)
total_amount = models.IntegerField()
amountreceived = models.IntegerField()
amount_left = models.IntegerField()
currency = models.ForeignKey(Currency, on_delete=models.CASCADE, null=True)
date = models.DateField(
blank=True, null=True)
paymentmethod = models.ForeignKey(
Allpaymentmethod, on_delete=models.CASCADE)
def __str__(self):
return str(self.payment_id)
所以,上面是我的 2 個模型的代碼。所以,例如,我想逐年獲取所有數據的總數
2019 年 = 總金額、收到的金額和剩余金額 2020 年 = 總金額、收到的金額和剩余金額...等...
以集群條形圖的形式顯示所有年份
對於名為date
的字段,您可以這樣做:
dates = Paymentdete.objects.filter(date__year='2021')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.