Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

chart.js - how to filter year from datefield in django

Basically I want to use 'chartjs' for display "Clustered Bar chart", with following things year wise

Total amount Amount Received Amount Left

I want to fetch data from data base and display it on chart, I have saved data of all above field 3 fields in one table for each date and I want to also fetch year from Datefield.

Main things is how to display Clustered bar chart by fetching data from database for each year.


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)


So, above is the code of my 2 models.So, i want to get total of all data yearwise for example

for year 2019= Total Amount,Amount Received,and amount Left for year 2020= Total Amount,Amount Received,and amount Left ...etc...

for all years in form of clustered Bar Chart

question from:https://stackoverflow.com/questions/65641514/how-to-filter-year-from-datefield-in-django

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You do it like this for a field called date:

dates = Paymentdete.objects.filter(date__year='2021')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...