Я хочу заполнить диаграммуJ точно так же, как в примере: https://www.chartjs.org/docs/latest/charts/bar.html
Я получаю эту ошибку:
AttributeError at /timesheet/json-month/
Got AttributeError when attempting to get a value for field `working_hour` on serializer `TimesheetSerializer`.
The serializer field might be named incorrectly and not match any attribute or key on the `str` instance.
Original exception text was: 'str' object has no attribute 'working_hour'.
С этой моделью:
class Timesheet(models.Model):
owner = models.ForeignKey(User, on_delete = models.CASCADE)
title = models.CharField(max_length = 64, verbose_name = _("Title"))
date = models.DateField(default=datetime.datetime.now())
working_hour = models.FloatField(verbose_name = _("Working time"))
week = models.IntegerField(verbose_name = "working week")
def __str__(self):
return "{}".format(self.title)
def save(self, *args, **kwargs):
if not self.pk:
self.week = datetime.datetime.strptime("{}".format(self.date), "%Y-%m-%d").isocalendar()[1]
super(Timesheet, self).save(*args, **kwargs)
Я сериализирую свои данные так (на мой взгляд):
def get_month_hours(self):
cursor = connection.cursor()
cursor.execute("""
SELECT
MONTH(date),
SUM(working_hour)
FROM
timesheet_timesheet
WHERE
owner_id = %s
AND
date >= MONTH(CURRENT_TIMESTAMP) - 12
GROUP BY
MONTH(date)
""", [self.user])
row = cursor.fetchone()[0]
return str(row)
@api_view(['GET'])
def timesheet_total_per_month(request):
hours = ReturnHour(request.user.pk, datetime.datetime.now().isocalendar()[1])
timesheets = hours.get_month_hours()
serializer = TimesheetSerializer(timesheets, many=True, read_only = True)
return Response(serializer.data)
В мои serializers.py:
class TimesheetSerializer(serializers.ModelSerializer):
class Meta:
model = Timesheet
fields ='__all__'
def create(self, validated_data):
"""
Create and return a new `Snippet` instance, given the validated data.
"""
return Timesheet.objects.create(**validated_data)
def update(self, instance, validated_data):
instance.owner = validated_data.get('owner_id', instance.owner)
instance.date = validated_data.get('date', instance.date)
instance.title = validated_data.get('title', instance.title)
instance.working_hour = validated_data.get('working_hour', instance.working_hour)
return instance
Вы знаете причину?
спасибо завперед,