У меня одна проблема с форматированием ответа DRF
Я делюсь информацией о моей модели здесь
class Plan(AuditFields, Model):
id = models.AutoField(primary_key=True)
object_id = models.UUIDField(
unique=True, editable=False, verbose_name='Public identifier')
plan_name = models.CharField(max_length=255, blank=True)
team = models.ManyToManyField(
settings.AUTH_USER_MODEL, blank=True, related_name="%(app_label)s_%(class)s_team")
class PlanDetail(AuditFields, Model):
plan = models.ForeignKey(
Plan, on_delete=models.CASCADE, blank=True, null=True)
plan_stage = models.CharField(max_length=255, blank=True)
start_date = models.DateField(null=True, blank=True)
end_date = models.DateField(null=True, blank=True)
status_of_plan = models.CharField(max_length=255, blank=True)
Используя вышеприведенные модели, мне нужно создать такой ответ. Но я сталкиваюсь с некоторыми проблемами с этим
plan details : [
{
"plan_stage": "Sample1",
"status_of_plan": "Completed",
"start_date": "2019-07-01",
"end_date": "2019-07-01",
},
{
"plan_stage": "Sample2",
"status_of_plan": "Not Commenced",
"start_date": "2019-08-01",
"end_date": "2019-08-31",
},
{
"plan_stage": "Sample3",
"status_of_plan": "Not Commenced",
"start_date": "2019-09-01",
"end_date": "2019-09-30",
},
{
"plan_stage": "Sample4",
"status_of_plan": "Not Commenced",
"start_date": "2019-10-01",
"end_date": "2019-10-31",
},
{
"plan_stage": "Sample 5",
"status_of_plan": "Completed",
"start_date": "2019-11-01",
"end_date": "2019-11-30",
},
{
"plan_stage": "End Date (Final Report)",
"status_of_plan": "Completed",
"start_date": "2019-12-01",
"end_date": "2019-12-31",
}
]
plan :{
"id" : 1.
"object_id": 456789023456546,
"plan_name" : "Sample",
"team":['Sachin','Dravid']
}
Я использую два сериализатора и пробую его ..... Но это не работает в этом случае
class PlanningSerializer(ModelSerializer):
'''THis is for Model Plan '''
team = TeamSerializer(many=True, read_only=True)
class Meta:
model = Plan
exclude = ['deleted_at']
class PlanningDetailsSerializer(ModelSerializer):
plan = PlanningSerializer(read_only=True)
class Meta:
model = PlanDetail
exclude = ['deleted_at']
Но я сталкиваюсь одна проблема
Я получаю такой ответ
[
{
"plan_stage": "Sample1",
"status_of_plan": "Completed",
"start_date": "2019-07-01",
"end_date": "2019-07-01",
"plan": {
"id" : 1.
"object_id": 456789023456546,
"plan_name" : "Sample",
"team":['Sachin','Dravid']
}
},
{
"plan_stage": "Sample2",
"status_of_plan": "Not Commenced",
"start_date": "2019-08-01",
"end_date": "2019-08-31",
"plan": {
"id" : 1.
"object_id": 456789023456546,
"plan_name" : "Sample",
"team":['Sachin','Dravid']
}
},
{
"plan_stage": "Sample3",
"status_of_plan": "Not Commenced",
"start_date": "2019-09-01",
"end_date": "2019-09-30",
"plan": {
"id" : 1.
"object_id": 456789023456546,
"plan_name" : "Sample",
"team":['Sachin','Dravid']
}
},
{
"plan_stage": "Sample4",
"status_of_plan": "Not Commenced",
"start_date": "2019-10-01",
"end_date": "2019-10-31",
"plan": {
"id" : 1.
"object_id": 456789023456546,
"plan_name" : "Sample",
"team":['Sachin','Dravid']
}
},
{
"plan_stage": "Sample 5",
"status_of_plan": "Completed",
"start_date": "2019-11-01",
"end_date": "2019-11-30",
"plan": {
"id" : 1.
"object_id": 456789023456546,
"plan_name" : "Sample",
"team":['Sachin','Dravid']
}
},
{
"plan_stage": "Sample6",
"status_of_plan": "Completed",
"start_date": "2019-12-01",
"end_date": "2019-12-31",
"plan": {
"id" : 1.
"object_id": 456789023456546,
"plan_name" : "Sample",
"team":['Sachin','Dravid']
}
}
]
Как мне объединить два из них и создать новый сериализатор с новыми полями