django - DRF how can get count of rows from multiple models
问题描述
I have two models, how can I get the count of rows each of them(using DRF) by one request?
class Question(AbstractArticle):
title = models.CharField(max_length=256, unique=True)
class Service(models.Model):
name = models.CharField(max_length=256)
解决方案
您只需要像这样简单地获取计数并传递它:
from rest_framework.views import APIView
from models import Question,Service
from rest_framework import status
class GetCount(APIView):
#use your proper authentication/permission classes here
def get(self,request):
question_count=Question.objects.count()
service_count= Service.objects.count()
return Response({"question":question_count,"service":service_count},status=status.HTTP_200_OK)
推荐阅读
- typescript - 打字稿:如何拥有一组类型的项目
- ios - 在 iOS 中使用图像初始化时 GMVTextLineFeature 返回 null
- php - 如何将我的结果集转换为分组的多维数组,然后是 json 字符串?
- android - 是否可以使用重复的 Thread.sleep() 来创建延迟以接收短信
- visual-studio-code - 与调试器一起运行 Browsersync
- arrays - 如何检查 groovy 列表中的所有或任何元素是否满足条件
- python - 通过进程队列传递动态创建的 multiprocess.Event()
- mysql - 如何在子查询中使用第一个查询的结果
- r - 无法在 Mac 上使用 fileInput 上传多个文件
- python - 使用 Python 的 Excel