How to extract INT from Queryset

1k Views Asked by At

I am trying to create a graph that dynamically updates based on date and count of POs.

Model:

class JobOrder(models.Model):
    status = models.CharField('status', choices=STATUS, max_length=200, default="Waiting")
    job_order = models.IntegerField(primary_key=True)
    remarks = models.CharField(max_length=45, blank=True, null=True)
    rush_order = models.IntegerField()
    date_issued = models.DateField('date_issued', auto_now_add=True)
    date_required = models.DateField()
    client = models.ForeignKey(Client, on_delete= models.CASCADE)
    total_amount = models.FloatField()

Query from View:

POs = JobOrder.objects.values('date_issued__year', 'date_issued__month').annotate(c=Count('id')).values('c')

Passing it in a javascript code in HTML:

function salesComparisonChart() {
    var POComparisson = google.visualization.arrayToDataTable([
        ['Month', 'Count'],
        {% for POs in POs %}
            ['Month', parseInt('{{POs}}')],
        {% endfor %}
    ]);

With my current code, the data that is being passed looks like this:

{'c': 4}
{'c': 2}
{'c': 1}
1

There are 1 best solutions below

0
On

If you're just after the counts themselves, then you should be able to retrieve a flat list of them using values_list:

POs = JobOrder.objects.values(
    'date_issued__year', 'date_issued__month'
).annotate(c=Count('id')).values_list('c', flat=True)  # Retrieve a flat list

In your template you can then drop the parseInt, and perhaps also rename your loop variable to PO to make the name less confusing:

{% for PO in POs %}
    ['Month', {{ PO }}],
{% endfor %}