如何通过 Django Rest Framework 返回嵌套的 json
Posted
技术标签:
【中文标题】如何通过 Django Rest Framework 返回嵌套的 json【英文标题】:How to return nested json by Django Rest Framework 【发布时间】:2020-12-30 16:42:12 【问题描述】:我在 postgres 数据库中有这样的数据
我想创建rest api来返回这样的嵌套json
[
"machine": "MC1",
"work" : [
"title": "21TCE20200910",
"subTitle1": "INSERT CORE",
"subTitle2": "P20200910-001",
"subTitle3": "DRAW20200910",
"status": 0,
"delay": 2
,
"title": "21TCE20200910",
"subTitle1": "INSERT CORE",
"subTitle2": "P20200910-001",
"subTitle3": "DRAW20200912",
"status": 1,
"delay": 1
]
,
"machine": "MC2",
"work" : [
"title": "21TCE20200911",
"subTitle1": "SCREW",
"subTitle2": "P20200910-001",
"subTitle3": "DRAW20200910",
"status": 1,
"delay": 2
]
,
"machine": "MC3",
"work" : [
"title": "21TCE20200913",
"subTitle1": "INSERT FIX",
"subTitle2": "P20200910-001",
"subTitle3": "DRAW20200910",
"status": 0,
"delay": 1
]
]
目前我只能为每条记录返回正常的 json,但我想像上面一样分组并返回嵌套的 json,任何帮助将不胜感激
以下是我的代码 模型.py
从 django.db 导入模型 类 MachineSchedule(models.Model): machine = models.CharField(max_length=255) 标题 = models.CharField(max_length=255) subTitle1 = models.CharField(max_length=255) subTitle2 = models.CharField(max_length=255) subTitle3 = models.CharField(max_length=255) 状态 = 模型.IntegerField(1) 延迟 = 模型.IntegerField(1)views.py
从 django.shortcuts 导入渲染 从 rest_framework 导入泛型 从 .models 导入 MachineSchedule 从 .serializers 导入 MachineScheduleSerializer 类 MachineScheduleListAPIView(generics.ListCreateAPIView): serializer_class= MachineScheduleSerializer 模型 = MachineSchedule 字段 = '__all__'序列化器.py
从 rest_framework 导入序列化程序 从 .models 导入 MachineAllocate 类 MachineAllocateSerializer(serializers.ModelSerializer): 元类: 型号 = MachineAllocate 字段 = '__all__'【问题讨论】:
你能显示你试过的代码吗? 我已经把它贴在下面了,谢谢! 答案不是放置代码的地方。请将其移至您的问题。 【参考方案1】:#models
class Work(models.Model):
title = models.CharField(max_length=255)
subTitle1 = models.CharField(max_length=255)
subTitle2 = models.CharField(max_length=255)
subTitle3 = models.CharField(max_length=255)
status = models.IntegerField(1)
delay = models.IntegerField(1)
class MachineSchedule(models.Model):
machine = models.CharField(max_length=255)
work = models.ForeignKey(Work, on_delete=models.CASCADE, related_name='work')
#serializer
from rest_framework import serializers
from .models import *
class WorkSerializer(serializers.ModelSerializer):
class Meta:
model = MachineAllo
fields = '__all__'
class MachineScheduleSerializer(serializers.ModelSerializer):
work = WorkSerializer(many=True)
class Meta:
model = MachineAllo
fields = '__all__'
#views
from rest_framework import generics
from .models import MachineSchedule
from .serializers import MachineScheduleSerializer
class MachineScheduleListAPIView(generics.ListCreateAPIView):
serializer_class = MachineScheduleSerializer
model = MachineSchedule
fields = '__all__'
【讨论】:
感谢您的回答,但是当打开 api ProgrammingError 列 app_mc_schedule_machineschedule.work_id 不存在时会显示这样的错误以上是关于如何通过 Django Rest Framework 返回嵌套的 json的主要内容,如果未能解决你的问题,请参考以下文章
django.test.client 上的 Django rest 框架导入错误
无法使用 Django Rest 框架发送压缩的 gzip 数据
Django REST to React - 无需密码即可获取社交身份验证令牌
Django Rest:为啥访问被拒绝,尽管 AccessAny 被设置为权限?
尽管有 AllowAny 权限,django-rest-framework 在 POST、PUT、DELETE 上返回 403 响应