Answer the question
In order to leave comments, you need to log in
How to add extra key to json?
Hello.
How can I slightly change the json structure what the serializer gives me?
Actually here it is:
class SectionSerializer(serializers.ModelSerializer):
images = ImageSerializer(many = True, read_only = True)
images2 = Image2Serializer(many = True, read_only = True)
class Meta:
model = Sections
fields = ('id', 'name', 'audio1', 'audio2', 'images', 'images2')
[
{
"id": 1,
"name": "Lake1",
"audio1": "qwe",
"audio2": "qwe",
"images": [
{
"id": 1,
"file_name": "/images/66acc105-1c06-45aa-a3b8-5_S3USvg3.png"
"images2": [
{
"id": 1,
"file_name": "/images/66acc105-1c06-45aa-a3b8-5_S3USvg3.png"
},
{
"id": 2,
"file_name": "/images/66acc105-1c06-45aa-a3b8-5_S3USvg3.png"
}
]
}
]
Answer the question
In order to leave comments, you need to log in
Upd. If you get an object as a result with a structure:
var obj = {
"id": 1,
"name": "Lake1",
"audio1": "qwe",
"audio2": "qwe",
"images": [
{
"id": 1,
"file_name": "/images/66acc105-1c06-45aa-a3b8-5_S3USvg3.png"
}],
"images2":
[
{
"id": 1,
"file_name": "/images/66acc105-1c06-45aa-a3b8-5_S3USvg3.png"
},
{
"id": 2,
"file_name": "/images/66acc105-1c06-45aa-a3b8-5_S3USvg3.png"
}
]
}
obj.images.images2 = obj.images2;
delete obj.images2;
{
"id": 1,
"name": "Lake1",
"audio1": "qwe",
"audio2": "qwe",
"images": [
{
"id": 1,
"file_name": "/images/66acc105-1c06-45aa-a3b8-5_S3USvg3.png",
"images2":
[
{
"id": 1,
"file_name": "/images/66acc105-1c06-45aa-a3b8-5_S3USvg3.png"
},
{
"id": 2,
"file_name": "/images/66acc105-1c06-45aa-a3b8-5_S3USvg3.png"
}
]
}]
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question