Сохранение данных в многомерную модель с помощью django orm join
Я столкнулся с проблемой в Django У меня есть 2 модели "batch", "batchyeild",
эти 3 имеют различные формы и пользователи вводят данные в них Теперь я создаю еще одну форму "История", в которую пользователь будет вводить данные, но в моем бэкенде данные, которые будут поступать из "истории", будут распределяться в эти 3 таблицы модели DB
Теперь запрашиваемые данные истории являются
batch_status
commodity name
pesticide
actual produce
acerage
variety_id
end_date
Я буду распределенно спасен в 2 моделях
партия имеет
batch_status
commodity name
pesticide
actual produce
average
variety_id
и batchyeild имеет столбцы
end_date
вид обоих выглядит как
и сериализатор обеих, моделей выглядит как
class BatchSerializer(serializers.ModelSerializer):
"""
This Serializer is of Batch Model for Get, Post, Patch and Delete method.
Request Body for Post
[{
"acerage": 75, "batch_health": 75, "farm_id":6
}]
Example Response
[{
"id": 14, "start_date": null, "acerage": 75.0, "batch_health": 75, "commodity_id": null,
"commodity_variety_id": null, "stage": "germination", "farm_id": 6, "expected_delivery_date": null,
"current_pdd": null, "historic_pdd": null, "current_gdd": null, "historic_gdd": null,
"sub_farmer_id": null, "batch_status": "to_start", "updated_at": "2021-07-20T06:48:44.027868Z",
"created_at": "2021-07-20T06:48:44.027896Z", "updated_by_id": 45, "created_by_id": 45
}]
"""
farm_id = serializers.PrimaryKeyRelatedField(queryset=Farm.objects.all())
commodity_id = serializers.IntegerField()
# commodity_variety_id = serializers.IntegerField()
commodity_name = serializers.CharField(read_only=True)
batch_name = serializers.CharField(read_only=True)
batch_median_health = serializers.IntegerField(read_only=True)
class Meta:
model = Batch
fields = ['id', 'batch_name', 'start_date', 'acerage', 'batch_health', 'commodity_id', 'commodity_variety_id',
'stage', 'farm_id', 'expected_delivery_date', 'current_pdd', 'historic_pdd', 'current_gdd',
'historic_gdd', 'sub_farmer_id', 'batch_status', 'updated_at', 'created_at', 'updated_by_id',
'created_by_id', 'commodity_name', 'historical_yield_per_acre', 'expected_produce', 'actual_produce',
'sop_adherence', 'actual_yield_per_acre', 'batch_median_health', 'end_date', 'pending_tasks']
def to_representation(self, instance):
result = super().to_representation(instance)
return OrderedDict([(key, result[key]) for key in result if result[key] is not None])