Hi,
I have a problem with my API.
i’m creating simple API to load json to database.
Part of json:
....
"updateOn": "31-01-2021"
},
"simulations": [
{
"code": "option1",
"name": "option1_name",
"is_selected": "false",
"parameterValues": [
{
"parameter": "tenor",
"value": "36"
},
{
"parameter": "vatFinancing",
"value": "yes"
}
]
}
] ....
I dont now how can I save data from array “parameterValues”.
Right now I have 2 models
class QtSimulations(models.Model):
code_p = models.ForeignKey(QtOfferParam2, to_field="code", db_column="ext_id", on_delete=models.PROTECT, related_name="simulations")
name = models.CharField(max_length=50, db_column="name")
code_s = models.CharField(max_length=50, db_column="code", primary_key=True)
is_selected = models.CharField(max_length=10, db_column="is_selected")
class Meta
db_table = 'ZZ_QT_SIMULATIONS'
def __str__(self):
return self.code
class QtParameterValues2(models.Model):
simulation_code = models.ForeignKey(QtSimulations, to_field="code_s", db_column="simulation_code", on_delete=models.PROTECT, related_name="parameterValues", primary_key=True)
parameter = models.CharField(max_length=200, null=True, db_column="parameter")
value = models.CharField(max_length=10, null=True, db_column="value_p")
class Meta:
db_table = 'ZZ_QT_PARAMETER_VALUE_2'
def __str__(self):
return self.code
and serializer
class SimulationsSerializer(serializers.HyperlinkedModelSerializer):
parameterValues = ParameterValuesSerializer(many=True)
code = serializers.CharField(source = 'code_s')
class Meta:
model = QtSimulations
fields = ('code', 'name', 'is_selected', 'parameterValues')
class OfferDetailSerializer(serializers.HyperlinkedModelSerializer):
quotation = QuotationField(source='*')
simulations = SimulationsSerializer(many=True)
class Meta:
model = QtOfferParam2
fields = ['quotation', 'simulations']
def create(self, validated_data):
simulations_data = validated_data.pop('simulations')
print(simulations_data)
code = QtOfferParam2.objects.create(**validated_data)
for simulations_data in simulations_data:
simulations_data['code_p'] = code
QtSimulations.objects.create(**simulations_data)
return code
If I remove ‘parameterValues’ from “SimulationsSerializer” everything is save.
How can I save data from 'parameterValues’array to QtParameterValues2?
I tried do somthing similar to simulation but i get error
Direct assignment to the reverse side of a related set is prohibited. Use parameterValues.set() instead.
Please help me.