class ObtainItemSerializer(serializers.ModelSerializer):
uid_1c = serializers.SlugRelatedField(
source='nomenclature',
queryset=Nomenclature.objects.all(),
slug_field='uid_1c')
class Meta:
model = ObtainingDocumentItem
fields = ['amount', 'obtaining_doc_id', 'uid_1c']
class ObtainOdataSerializer(serializers.ModelSerializer):
manufacturer = serializers.SlugRelatedField(
slug_field='uid_1c',
queryset=Manufacturer.objects.all()
)
nomenclatures = ObtainItemSerializer(many=True)
class Meta:
model = ObtainingDocument
fields = ['uid_1c',
'number',
'created_on',
'manufacturer',
'nomenclatures']
def create(self, validated_data):
obtainitem_data = validated_data.pop('nomenclatures')
obtain = ObtainingDocument.objects.create(**validated_data)
item_list = [
ObtainingDocumentItem(
amount=item['amount'],
nomenclature=item['nomenclature'],
obtaining_doc=obtain,
) for item in obtainitem_data
]
ObtainingDocumentItem.objects.bulk_create(item_list)
return obtain
def create_obtain(obtainingdocument_list):
"""Создание ObtainingDocument"""
for item in obtainingdocument_list:
serializer = ObtainOdataSerializer(data=item)
if serializer.is_valid():
serializer.save()
return obtainingdocument_list
[
{
"uid_1c": "bea7e63e-44b1-11ed-aa45-ac1f6bd30990",
"number": "ПП00-003591",
"created_on": "20221005",
"manufacturer": "dc11d0e8-bb9a-11eb-aa29-ac1f6bd30991",
"nomenclatures": [
{
"obtaining_doc": "bea7e63e-44b1-11ed-aa45-ac1f6bd30990",
"uid_1c": "186c5550-9f7f-11ec-aa3a-ac1f6bd30991",
"number": "1",
"amount": 202
}
]
},
{
"uid_1c": "e5db8336-4571-11ed-aa45-ac1f6bd30990",
"number": "ПП00-003614",
"created_on": "20221006",
"manufacturer": "dc11d0e8-bb9a-11eb-aa29-ac1f6bd30991",
"nomenclatures": [
{
"obtaining_doc": "e5db8336-4571-11ed-aa45-ac1f6bd30990",
"uid_1c": "988be3d3-6ef9-11ec-aa39-ac1f6bd30991",
"number": "1",
"amount": 72
},
{
"obtaining_doc": "e5db8336-4571-11ed-aa45-ac1f6bd30990",
"uid_1c": "460e2506-72db-11ec-aa39-ac1f6bd30991",
"number": "2",
"amount": 50
}
]
}
]
<slug:service_slug>