我使用下面给出的 taskIdSerializer 创建数据的方法:
def to_representation(self, instance):
result = super(taskIdSerializer, self).to_representation(instance)
result = OrderedDict([(key, result[key]) for key in result if result['date']])
return result
这个结果在我的函数中返回,如下所示:
[
{
"description": "asdsa",
"priority_id": 3,
"name": "tetst",
"date": [
{
"dates": "20/09/2021 15:14:00",
"id": 146
},
{
"dates": "20/09/2021 15:14:00",
"id": 145
}
]
},
{}, // this is value add dict when date = []
{} // this is value add dict when date = []
]
我想删除 {} 对象,我想得到这样的返回数据:
[
{
"description": "asdsa",
"priority_id": 3,
"name": "tetst",
"date": [
{
"dates": "20/09/2021 15:14:00",
"id": 146
},
{
"dates": "20/09/2021 15:14:00",
"id": 145
}
]
}
]