Django def list API join two table drom DB

Я пытаюсь использовать функцию def list Django из этих двух, которые у меня есть таблиц Batch и BatchYield

таблица Batch выглядит так

batch_id | batch_status  |
`````````|```````````````|
11       |   completed   |

а таблица BatchYield выглядит как

id | grade_a_produce | grade_b_produce | grade_c_rejection | harvest_date | batch_id |
```|`````````````````|`````````````````|```````````````````|``````````````|``````````|
23 |     100         |       120       |        212        | 22-02-12     |    11    |
25 |     110         |       122       |        242        | 21-01-14     |    11    |

Поэтому я написал функцию def list в Django, в которой я объединил эти две таблицы с помощью этого кода


    def list(self, request, *args, **kwargs):
        try:
            for data in request.data:
                batch_id = data.get('batch_id')
                all_batchyield = BatchYield.objects.filter(batch_id=batch_id).values('grade_a_produce', 'id',
                                                                                     'grade_b_produce',
                                                                                     'grade_c_rejection',
                                                                                     'harvest_date', 'batch_id')
                if all_batchyield.count == 0:
                    return Response({"response": "Data not Found"}, status=status.HTTP_200_OK)
                all_batchyield_df = pd.DataFrame(all_batchyield)
                all_batchyield_df = all_batchyield_df.replace({np.nan: None})

                all_completed_batchs = Batch.objects.filter(id=batch_id).values('batch_status', 'id')
                completed_batch_df = pd.DataFrame(all_completed_batchs)
                completed_batch_df = completed_batch_df.replace({np.nan: None})
                completed_batch_df.rename(columns={'id': 'batch_id'}, inplace=True)
                final = pd.merge(completed_batch_df, all_batchyield_df, on='batch_id')
                final = final.drop('batch_id', axis=1)

        except Exception as e:
            return Response({"response": str(e)}, status=status.HTTP_400_BAD_REQUEST)
        return Response(final.to_dict('record'), status=status.HTTP_200_OK)

Из этого кода я получил вывод, который выглядит так

[
    {
        "batch_status": "completed",
        "grade_a_produce": 100.0,
        "id": 23,
        "grade_b_produce": 120.0,
        "grade_c_rejection": 212.0,
        "harvest_date": "2022-02-12T00:00:00Z"
    },
    {
        "batch_status": "completed",
        "grade_a_produce": 110.0,
        "id": 25,
        "grade_b_produce": 122.0,
        "grade_c_rejection": 242.0,
        "harvest_date": "2022-01-14T00:00:00Z"
    }
]

Но это не тот результат, который я хочу получить Я хочу преобразовать grade_a_produce в две части

| grade_a_produce |          | batch_produce | grade     |
|`````````````````|    ==>   |```````````````|```````````|
|     100         |          |     100       | "grade_a" |
|     110         |          |     110       | "grade_a" |

и то же самое для оценки_b_produce и оценки_c_rejection

Итак, желаемый вывод будет выглядеть как

[
    {
        "id": 23,
        "batch_produce": "100",
        "batch_id": "11",
        "harvest_date": "2022-02-12",
        "grade": "grade_a",
        "batch_status": "completed"
    },
    {
        "id": 23,
        "batch_produce": "120",
        "batch_id": "11",
        "harvest_date": "2022-02-12",
        "grade": "grade_b",
        "batch_status": "completed"
    },
    {
        "id": 23,
        "batch_produce": "212",
        "batch_id": "11",
        "harvest_date": "2022-02-12",
        "grade": "grade_c",
        "batch_status": "completed"
    },
    {
        "id": 25,
        "batch_produce": "110",
        "batch_id": "11",
        "harvest_date": "2021-01-14",
        "grade": "grade_a",
        "batch_status": "completed"
    },
    {
        "id": 25,
        "batch_produce": "122",
        "batch_id": "11",
        "harvest_date": "2021-01-14",
        "grade": "grade_c",
        "batch_status": "completed"
    },
    {
        "id": 25,
        "batch_produce": "242",
        "batch_id": "11",
        "harvest_date": "2021-01-14",
        "grade": "grade_c",
        "batch_status": "completed"
    },
]
Вернуться на верх