Django + Postgre + highcharts

问题描述 投票:1回答:1

如何计算来自Django的PostgreSQL数据库上的行并使用highchart显示?

例如:我想显示第二天从早上7.00到早上7.00有多少记录/行。

型号:

from django.db import models
   from datetime import datetime, date

class hujan(models.Model):
    id = models.AutoField(primary_key=True)
    tanggal = models.DateTimeField(auto_now_add=True)
    dtinms = models.IntegerField()
    hujan = models.FloatField()

序列化器:

from rest_framework import serializers
from .models import hujan, cahayasuhukelembapan

class hujanSerializer(serializers.ModelSerializer):
    class Meta:
        model = hujan
        fields = ('tanggal','dtinms','hujan')

意见:

from django.shortcuts import render, get_object_or_404
from django.http import HttpResponse, JsonResponse
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.renderers import TemplateHTMLRenderer
from .models import hujan
from .serializers import hujanSerializer

def homePageView(request):
  return render(request,'homepage.html')

class hujanlistall(APIView):
    def get(self, request):
        Hujan = hujan.objects.all()
        serializer = hujanSerializer(Hujan, many=True)
        return JsonResponse(serializer.data,safe=False)
django postgresql highcharts django-rest-framework countrows
1个回答
0
投票

修改“ views.py”文件,

import datetime
class hujanlistall(APIView):
    def get(self, request):
        Hujan = hujan.objects.filter(tanggal__range = (datetime.datetime.combine(start_date,datetime.time.min),datetime.datetime.combine(end_date, datetime.time.max)))
        RowCount = len(Hujan)
        serializer = hujanSerializer(Hujan, many=True)
        return JsonResponse(serializer.data,safe=False)
© www.soinside.com 2019 - 2024. All rights reserved.