We are using Django Rest framework to serialise and deserialise json response from a third party API. I need to translate the string field values to a numeric value in our system.
The JSON response is given below
{
"result": "win" # Other values include "loss"/"inconclusive"/"pending"
}
Django Model
class Experiment(Model):
RESULTS = (
(1, 'win'),
(2, 'loss'),
(3, 'inconclusive'),
)
inferred_result = models.IntegerField(choices=RESULTS, null=True, blank=True)
Django Serializer Class
class ResultsSeializer(serializers.ModelSerializer):
# Need to convert "result" from "string" to "int" and vice versa.
class Meta:
model = models.Experiment
I want to convert the inferred_result
integer value to string
equivalent and vice versa. I could use this approach: Django rest framework. Deserialize json fields to different fields on the model if this is a good idea to convert integers to string. How do I convert string to int? I am new to django rest api and django.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…