Django Rest Framework not pulling through data

I'm trying to get my data to pull through from my model using DRF. It's my first time using it so I'm trying to work out what I'm doing wrong, the below view pulls through a blank list in the API for "chartdata". Can anyone help with what I'm doing wrong? I would like to get the data pulling through first, and my ultimate goal is to pull through my data and display it on a chart with chart.js on the 'health_hub_tracker' view. One step at a time! Any help would be appreciated!

enter image description here

views.py:

class ChartData(APIView):
    authentication_classes = []
    permission_classes = []

    def get(self, request, format=None):
        labels = "weight (lbs)"
        chartLabel = "weight (lbs)"
        serialized_stats = []
        for stats in HealthStats.objects.filter(user=request.user.id):
            serialized_stats.append({
                "weight": stats.weight,
                })
        data = {
                    "labels": labels,
                    "chartLabel": chartLabel,
                    "chartdata": serialized_stats,
            }
        return Response(data)

models.py:

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from cloudinary.models import CloudinaryField


class HealthStats(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    date = models.DateTimeField(auto_now_add=True)
    weight = models.DecimalField(max_digits=5, decimal_places=2)
    run_distance = models.DecimalField(max_digits=5, decimal_places=2)
    run_time = models.DurationField()

    class Meta:
        db_table = 'health_stats'
        ordering = ['-date']

    def get_absolute_url(self):
        return reverse('HealthHub:health_hub_history')

    def __str__(self):
        return f"{self.user} | {self.date}"


class Article(models.Model):
    title = models.CharField(max_length=200, unique=True)
    topic = models.CharField(max_length=200)
    featured_image = CloudinaryField('image', default='placeholder')
    content = models.TextField()

    def __str__(self):
        return f"{self.title}"

urls.py:

from django.urls import path
from django.contrib.staticfiles.storage import staticfiles_storage
from django.views.generic.base import RedirectView
from . import views

app_name = 'HealthHub'

urlpatterns = [
    path('', views.home, name='home'),
    path('MyHealth/', views.health_hub, name='health_hub'),
    # path('MyHealth/history/tracker', views.health_hub_tracker, name='health_hub_tracker'),
    path('MyHealth/update', views.UpdateHealth.as_view(), name='health_hub_update'),
    path('MyHealth/history', views.health_history, name='health_hub_history'),
    path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url("favicon.ico"))),
    path('MyHealth/delete/<int:pk>', views.DeleteEntry.as_view(), name='health_hub_delete'),
    path('MyHealth/edit/<int:pk>', views.EditHealth.as_view(), name='health_hub_edit'),
    path('article/<item_id>', views.article_detail, name='health_hub_article'),
    path('api', views.ChartData.as_view(), name='health_hub_tracker'),
]

health_hub_tracker.html:

{% extends 'base.html' %}
{% load static %}

{% block content %}
<h1>Weight Tracker</h1>
<canvas id="myChartline" width="400" height="200"></canvas>
<script>
    const endpoint = '/api';

    $.ajax({
        method: "GET",
        url: endpoint,
        success: function(data) {
            drawLineGraph(data, 'myChartline');
            console.log("Creating");
        },
        error: function(error_data) {
            console.log(error_data);
        }
        })

        function drawLineGraph(data, id) {
            const labels = data.labels;
            const chartLabel = data.chartLabel;
            const chartdata = data.chartdata;
            const ctx = document.getElementById('myChart').getContext('2d');
            const myChart = new Chart(ctx, {
            type: 'line',
            data: {
                labels: ['Weight'],
                datasets: [{
                    label: 'Weight (lbs)',
                    data: [chartdata],
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(255, 206, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(255, 159, 64, 0.2)'
                    ],
                    borderColor: [
                        'rgba(255, 99, 132, 1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(153, 102, 255, 1)',
                        'rgba(255, 159, 64, 1)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });
</script>
{% endblock content %}
Back to Top