gpt4 book ai didi

django - 如何计算 Django 中 PostgreSQL 数据库的行数?

转载 作者:行者123 更新时间:2023-11-29 13:38:41 30 4
gpt4 key购买 nike

如何在Django中统计PostgreSQL数据库的行数并使用highchart显示?

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

模型:

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)

最佳答案

修改“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)

关于django - 如何计算 Django 中 PostgreSQL 数据库的行数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58707039/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com