【发布时间】:2021-03-30 15:51:12
【问题描述】:
在将Generics.ListAPIView 类返回的数据发送到前端之前,您如何对其进行格式化?我想将元数据添加到返回值,但我无法添加任何不属于模型的元数据。例如:
class someList(generics.ListAPIView):
serializer_class = someSerializer
def get_queryset(self):
return someQueryset()
class someListSerializer(SurveySerializer):
class Meta:
model = someListModel
fields = ['modelField']
class someListModel(Base):
modelField=models.TextField(default="", blank=True)
这将产生 [{modelField:information},{modelField:information},{modelField:information}]
如果我想要怎么办 [ {modelField:information, informationCalculatedFromQueryset:butNotPartOfModel}, {modelField:information, informationCalculatedFromQueryset:butNotPartOfModel},{modelField:information, informationCalculatedFromQueryset:butNotPartOfModel} ]
informationCalculatedFromQueryset 不是 someListModel 的一部分,所以我不能只将它添加到字段中。如何手动将此信息附加到前端?这可能吗?
【问题讨论】:
标签: django django-models django-rest-framework django-views django-serializer